GWT Junit TestCase

时间:2012-09-28 10:10:51

标签: java gwt junit testcase

我想用GWT Junit测试RPC。我使用标准GWT示例并创建以下测试用例:

public class TestGreetingService extends GWTTestCase {
/**
 * Must refer to a valid module that sources this class.
 */
public String getModuleName() {
    return "com.TestGreeting";
}   

/**
 * This test will send a request to the server using the greetServer method
 * in GreetingService and verify the response.
 */
public void testGreetingService() {
    // Create the service that we will test.
    GreetingServiceAsync greetingService = GWT
            .create(GreetingService.class);
    ServiceDefTarget target = (ServiceDefTarget) greetingService;
    target.setServiceEntryPoint(GWT.getModuleBaseURL() + "TestGreeting/greet");

    // Since RPC calls are asynchronous, we will need to wait for a response
    // after this test method returns. This line tells the test runner to
    // wait
    // up to 10 seconds before timing out.
    delayTestFinish(20000);

    // Send a request to the server.
    greetingService.greetServer("GWT User", new AsyncCallback<String>() {
        public void onFailure(Throwable caught) {
            // The request resulted in an unexpected error.
            fail("Request failure: " + caught.getMessage());
        }

        public void onSuccess(String result) {
            // Verify that the response is correct.
            assertTrue(result.startsWith("Hello, GWT User!"));

            // Now that we have received a response, we need to tell the
            // test runner
            // that the test is complete. You must call finishTest() after
            // an
            // asynchronous test finishes successfully, or the test will
            // time out.
            finishTest();
        }
    });
}
}

不幸的是我收到以下错误:有人可以帮助我吗?我还上传了项目:goo.gl/UnFyt

 [WARN] 404 - POST /com.TestGreeting.JUnit/TestGreeting/greet (192.168.1XX.XX) 1427 bytes <br>
Request headers <br>
Host: 192.168.1XX.XX:53577 <br>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.19) Gecko/2010031422 <br> Firefox/3.0.19 <br>
Accept-Language: en-us <br>
Accept: */* <br>
Connection: Keep-Alive <br>
Referer: http://192.168.1XX.XX:53577/com.Test...8.1XX.XX:53572 <br>
X-GWT-Permutation: HostedMode <br>
X-GWT-Module-Base: http://192.168.1XX.XX:53577/com.TestGreeting.JUnit/ <br>
Content-Type: text/x-gwt-rpc; charset=utf-8 <br>
Content-Length: 181 <br>
Response headers <br>
Content-Type: text/html; charset=iso-8859-1 <br>
Content-Length: 1427 <br>

1 个答案:

答案 0 :(得分:1)

您收到404警告,无法找到您的服务。 ServiceEntryPoint定义一定有问题。

尝试同步接口的@RemoteServiceRelativePath(“greet”)注释或将setServiceEntryPoint更正为

    target.setServiceEntryPoint(GWT.getModuleBaseURL() + "greet");

另外看一下DevGuide,那里的说明帮助我很好地习惯了GWT RPC服务: https://developers.google.com/web-toolkit/doc/2.4/DevGuideServerCommunication#DevGuideImplementingServices