我在win XP上使用Eclipse Indigo,并试图为我的GWT 2.4应用程序编写GWT测试用例。具体来说,我试图测试一个AJAX请求,但得到一个404。我以为GWT会在托管模式下启动自己的服务器?我的代码是
public class GetHtmlTest extends GWTTestCase {
public void gwtSetUp() {
...
submitButton = new Button();
DOM.setElementAttribute(submitButton.getElement(), "id", Productplus_gwt.SUBMIT_BUTTON_ID);
...
}
@Test
public void testSuccessEvent() {
nameField.setText(VALID_ID);
submitButton.click();
Timer timer = new Timer() {
public void run() {
final Element contentDiv = DOM.getElementById(Productplus_gwt.CONTENT_DIV_ID);
final String divText = contentDiv.getInnerText();
assertNotNull(divText);
assertEquals(-1, divText.toLowerCase().indexOf("error") );
finishTest();
}
};
timer.schedule(100);
delayTestFinish(2000);
} // testSuccessEvent
最终,单击按钮会导致这个AJAX调用…
productPlusService.getHtml(docId, new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
submitButtonElement.setAttribute("enabled", Boolean.TRUE.toString());
contentDiv.setInnerHTML("<span>Error: " + caught.getMessage() + "</span>");
}
public void onSuccess(String result) {
submitButtonElement.setAttribute("enabled", Boolean.TRUE.toString());
contentDiv.setInnerHTML(result);
// Format tabs
postHtmlProcessing();
}
});
我通过右键单击它来运行测试,选择"运行为"one_answers"GWT测试用例"。控制台上的错误是
[WARN] 404 - POST /com.myco.clearing.productplus.Productplus_gwt.JUnit/getHtml (10.40.70.197) 1444 bytes
Request headers
Host: 10.40.70.197:2084
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.19) Gecko/2010031422 Firefox/3.0.19
Accept-Language: en-us
Accept: */*
Connection: Keep-Alive
Referer: http://10.40.70.197:2084/com.myco.clearing.productplus.Productplus_gwt.JUnit/junit-standards.html?gwt.codesvr=10.40.70.197:2080
X-GWT-Permutation: HostedMode
X-GWT-Module-Base: http://10.40.70.197:2084/com.myco.clearing.productplus.Productplus_gwt.JUnit/
Content-Type: text/x-gwt-rpc; charset=utf-8
Content-Length: 217
Response headers
Content-Type: text/html; charset=iso-8859-1
Content-Length: 1444
你知道出了什么问题吗?谢谢,- Dave
对于JUnit测试,您必须使用<servlet path="..." class="..." />
元素在模块的gwt.xml
中声明servlet。