unit-testing - 如何为 GWT servlet 编写测试?

标签 unit-testing gwt servlets gwt-rpc

我有一个 GWT 应用程序,服务器端很少有 servlet。我想测试这些 servlet(不需要使用 Selenium 或任何其他基于 Web 的框架进行 GUI 测试)。或者换句话说,我希望测试模拟 GWT 的客户端。

测试 servlet 的自然挑战是:

  • 启动网络服务器,
  • 模拟客户端,
  • Servlet 立即返回,将值传递给 AsyncCallback 对象。

  • 到目前为止,我已经能够弄清楚(尽管这尚未经过测试),即:
    1.我可以通过扩展GWTTestCase来启动容器
    3. 我找到了一个关于 asynchronous testing 的谷歌文档,因此可以等待异步回调。谷歌文档也提到了这一点:

    Server side testing

    The tests described above are intended to assist with testing client side code. The test case wrapper GWTTestCase will launch either a development mode session or a web browser to test the generated JavaScript. On the other hand, server side code runs as native Java in a JVM without being translated to JavaScript, so it is not necessary to run tests of server side code using GWTTestCase as the base class for your tests. Instead, use JUnit's TestCase and other related classes directly when writing tests for your application's server side code. That said, you may want both GWTTestCase and TestCase coverage of code that will be used on both the client and the server.



    但是没有示例或更深入的解释如何实现这一点。

    我还没有想出如何模拟客户端...任何想法我该怎么做?

    或者,如果这不是这样做的方法,还有其他方法吗?我更喜欢使用 native GWT 类,而不是一些用于测试 servlet 的 3rd 方框架。

    谢谢!

    最佳答案

    如何使用嵌入式 Jetty 实例...无论如何,Jetty 服务器都包含在 GWT SDK 中。因此,只需将 gwt-dev.jar 包含在您的项目中,然后您就可以进入服务器端了。模拟客户端是一个完全不同的故事。问题是在 GWT 魔法中发生的 JavaScript 到 Java 序列化/反序列化......

    有一个名为 gwt-syncproxy 的项目可以在这里提供帮助:
    http://code.google.com/p/gwt-syncproxy/

    在代码中,这可能如下所示:

    import junit.framework.Assert;
    
    import org.junit.BeforeClass;
    import org.junit.Test;
    
    import org.mortbay.jetty.Server;
    import org.mortbay.jetty.servlet.Context;
    import org.mortbay.jetty.servlet.ServletHolder;
    
    import com.gdevelop.gwt.syncrpc.SyncProxy;
    
    public class ServletTest {
    
        private Server _server;
    
        @BeforeClass
        public void setUp() throws Exception {
            _server = new Server(8080);
            Context root = new Context(_server, "/", Context.SESSIONS);
            root.addServlet(new ServletHolder(new MyServiceImpl()), "/servlet");
            _server.start();
        }
    
        @Test
        public void testMethod1() throws Exception {
            MyService rpcService = (MyService) SyncProxy.newProxyInstance(MyService.class, "http://127.0.0.1:8080/servlet", "testMethod1");
    
            String result = rpcService.testMethod1();
    
            Assert.assertTrue(result != null);
        }
    
    }
    

    关于unit-testing - 如何为 GWT servlet 编写测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5328838/

    相关文章:

    unit-testing - Grails 1.3.3:未填充controller.redirectArgs.action

    unit-testing - 这段代码可测试吗?

    jquery - GWT/GXT 标签编辑器?

    java - 如何将两个 Json 列表从 Java Servlet 传递到 HTML 页面?

    java - maven - 无法执行 cargo :start

    javascript - Controller 测试模拟工厂

    java - 是否有使用 Groovy 进行单元测试(以及集成或回归,如果适用)的情况?

    java - 在 GWT 中使用模拟堆栈有多少开销?

    java - 在表格单元格中创建链接并通过单击打开 float 弹出窗口

    java - SocialAuth java 库未按预期工作(使用 NetBeans)