I have Java servlets that rely on the Channel API, from Google App Engine. I am trying to write unit tests for that code, but I am quite stumped with how to write a stub client to receive a response message from the servlet, instead of me always having to rely on using an actual web page as my test client. My servlet is quite simple:
ChannelService channelService = ChannelServiceFactory.getChannelService(); channelService.sendMessage(new ChannelMessage(someKey, "ECHO: " + someMsg));
Usually, I read this in my test client, which is a web page. But, I want to write unit tests that I can test using a framework like jUnit or TestNG. After reading Google's page about unit testing their Java services, I tried stuff such as using a LocalServiceTestHelper object in my test class and configure it with a LocalChannelServiceTestConfig object. The result of this is that my test class can create an instance of
LocalChannelService, a class which seems to implement the same methods as
- Neither classes have any robust relationship to each other (don't implement a similar interface, are not in the same class hierarchy...). Makes it impossible for me to inject a LocalChannelService object into my servlet as a mock object.
- No documentation whatsoever on LocalChannelService API (Thanks a lot, Google).
- Also no documentation on their Local Unit Testing page for how to test the Channel API (again, thanks a lot Google)
So then I came across this post about how to test
ChannelService in Java code. Unfortunately, the only relevant answer given was for Python. Well, lo and behold, Google indeed does provide Python GAE server developers with tools for writing stub clients for Google services, as outline in their Python Local Unit Testing guide. Unbelievable how they neglected to do the same for Java!
Used a better name, "Google App Engine" instead of just "App Engine"
These are just my thoughts on unit testing the Channel API:
You don't need a mock. Just use the actual API. Just pretend that it works. It doesn't return any failure even if your message fails to send, so you can't test for that anyways.
Have a backup. I don't think you can rely on the Channel API. Messages fail to appear. I personally believe you need a backup, like a polling API that isn't as responsive as the Channel API, but at least works.
Unit test your backup API.
Use Selenium to test the Channel API in a browser.