Reputation: 528
I have read the AppEngine unit testing guidelines, and I got the Java Datastore service tests working, but I'm not having any luck with the Channel service. The guide doesn't give any specific examples for channel testing, and the javadocs aren't of much help either, but my IDE is showing me some classes that seem to be meant for unit testing a local channel service; I just can't figure out how to use them.
Does anyone have any experience or examples testing the GAE Channel Service?
Upvotes: 3
Views: 550
Reputation: 11463
Ivan provided neat Python example, here's a solution for Java.
Supppose here's the code we want to test
public class ClientChannelService {
private ChannelService channelService = ChannelServiceFactory.getChannelService();
public String createToken(){
return channelService.createChannel(UUID.randomUUID().toString());
}
public void sendMessage(String token, String message){
channelService.sendMessage(new ChannelMessage(token, message));
}
}
First, add appengine-testing.jar
to the classpath:
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-testing</artifactId>
<version>${appengine.version}</version>
<scope>test</scope>
</dependency>
Next, create a test as follows. I'm assuming JUnit here, but generaly you are free to use any test framework, it doesn't matter for GAE.
private ClientChannelService service;
private LocalServiceTestHelper helper = new LocalServiceTestHelper(
new LocalChannelServiceTestConfig());
private ChannelManager channelManager;
@Before
public void setUp() {
helper.setUp();
channelManager = LocalChannelServiceTestConfig.getLocalChannelService()
.getChannelManager();
service = new ClientChannelService();
}
@After
public void tearDown() {
helper.tearDown();
}
@Test
public void testSendMessage() {
String token = service.createToken();
connectionId = channelManager.connectClient(token); //emulate client connection
service.sendMessage(token, "message");
String message = channelManager.getNextClientMessage(token, connectionId);
assertEquals("message", message);
}
Upvotes: 1
Reputation: 21
The following has worked for me:
import unittest
from google.appengine.api import channel
from google.appengine.ext import testbed
class TestCase(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_channel_stub()
def test_send(self):
channel_stub = self.testbed.get_stub('channel')
token = channel.create_channel('ClientID1')
channel_stub.connect_channel(token)
channel.send_message('ClientID1', 'hello')
channel_messages = channel_stub.get_channel_messages(token)
channel_stub.clear_channel_messages(token)
self.assertEquals(['hello'], channel_messages)
if __name__ == '__main__':
unittest.main()
You can also look at the source code for the channel service stub.
Upvotes: 2
Reputation: 3639
Have you seen the example at http://code.google.com/p/googleappengine/source/browse/trunk/java/src/main/com/google/appengine/tools/development/testing/LocalChannelServiceTestConfig.java?r=182
Oops sorry for the previous post, I realized you aren't doing capabilities...
Upvotes: 0