Search code examples
javaunit-testinggoogle-app-enginechannel-api

How to unit test code relying on Channel API (Google App Engine, Java)


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 ChannelService.

However:

  • 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!


Now that I've gotten all that off my chest (apologies if I sounded a bit too flustered), I would really like to know, what is the best way for testing Java code relying on Google App Engine's Channel API? I really wish to rely on testing tools instead of web pages. do I have to somehow call Google's Javascript Client for Channel API from Java? There must be a better way in which I can use a mock object or stub.


Update 1

Used a better name, "Google App Engine" instead of just "App Engine"


Solution

  • These are just my thoughts on unit testing the Channel API:

    1. 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.

    2. 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.

    3. Unit test your backup API.

    4. Use Selenium to test the Channel API in a browser.