我已经阅读了AppEngine unit testing guidelines,我得到了Java数据存储区服务测试,但我对Channel服务没有任何好运。该指南没有给出任何特定的通道测试示例,并且javadoc也没有太大帮助,但是我的IDE向我展示了一些似乎用于单元测试本地通道服务的类;我无法弄清楚如何使用它们。
有没有人有测试GAE频道服务的经验或示例?
答案 0 :(得分:2)
以下对我有用:
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()
答案 1 :(得分:1)
Ivan提供了巧妙的Python示例,这是Java的解决方案。
这里是我们想要测试的代码
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));
}
}
首先,将appengine-testing.jar
添加到类路径中:
<dependency>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-testing</artifactId>
<version>${appengine.version}</version>
<scope>test</scope>
</dependency>
接下来,按如下方式创建测试。我在这里假设JUnit,但通常你可以自由使用任何测试框架,它对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);
}
答案 2 :(得分:0)