我在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();
}
});
我通过右键单击它,选择“Run As”和“GWT Test Case”来运行测试。控制台中的错误是
[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
任何想法出了什么问题?谢谢, - 戴夫
答案 0 :(得分:4)
对于JUnit测试,您必须使用<servlet path="..." class="..." />
element在模块的gwt.xml
中声明您的servlet。