采用GWT提供的单元测试类,可以在不启动host模式的情况下测试客户端代码.对指定的模块测试其客户端的所有代码.
在Eclipse下单元测试GWT的客户端代码的方法:
Creating a Test Case by HandjunitCreator
If you prefer not to use the , you may create a test case suite by hand by following the instructions below:
- Define a class that extends GWTTestCase. Make sure your test class is on the module source path (e.g. in the client subpackage of your module.) You can add new source paths by editing the and adding a <source> element.
- If you do not have a GWT module yet, create a that causes the source for your test case to be included. If you are adding a test case to an existing GWT app, you can just use the existing module.
- Implement the method GWTTestCase.getModuleName() to return the fully-qualified name of the module. This is the glue that tells the JUnit Test case which module to instantiate.
- Compile your test case class to bytecode. You can use the Java compiler directly using javac or a Java IDE such as Eclipse.
- Run your test case. Use the class junit.textui.TestRunner as your main class and pass the full name of your test class as the command line argument, e.g. com.example.foo.client.FooTest. When running the test case, make sure your classpath includes:
- Your project's src directory
- Your project's bin directory
- The gwt-user.jar library
- One of gwt-dev-windows.jar, gwt-dev-linux.jar, or gwt-dev-mac.jar depending on your platform
- The junit.jar library
我的例子:
package com.google.gwt.sample.kitchensink.client.test;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.sample.kitchensink.client.Foo;
import com.google.gwt.sample.kitchensink.client.KitchenSink;
import com.google.gwt.sample.kitchensink.client.MySink;
import com.google.gwt.sample.kitchensink.client.Sink.SinkInfo;
import com.google.gwt.sample.kitchensink.client.service.MyService;
import com.google.gwt.sample.kitchensink.client.service.MyServiceAsync;
import com.google.gwt.sample.kitchensink.client.service.MyServiceAsync.MyServiceClass;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
public class GwtTestCaseTest extends GWTTestCase {
public String getModuleName() {
// TODO Auto-generated method stub
return "com.google.gwt.sample.kitchensink.KitchenSink";
}
public void testMyButton() {
System.out.println("============b=============");
Foo myModule = new Foo();
assertTrue(myModule.exampleValidator("foo"));
assertTrue(myModule.exampleValidator("Foo"));
assertTrue(myModule.exampleValidator("FOO"));
assertTrue(myModule.exampleValidator("fOo"));
}
public void testKitchenSink(){
KitchenSink sink = new KitchenSink();
sink.onModuleLoad();
final HTML html = new HTML();
MyServiceAsync myServiceAsync = MyServiceClass.getMyServiceAsync();
myServiceAsync.login("diaoge", "12345", new AsyncCallback(){
public void onFailure(Throwable caught) {
html.setHTML("未能连接服务器");
}
public void onSuccess(Object result) {
Integer res = (Integer)result;
int i = res.intValue();
if(i == MyService.LOGIN_SUCCESS){
html.setHTML("登录成功");
}else{
html.setHTML(i+"");
}
}
});
}
}
在Eclipse单元测试的配置
<?xml version="1.0" encoding="UTF-8"?> <launchConfiguration type="org.eclipse.jdt.junit.launchconfig"> <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS"> <listEntry value="/com.hotye.gwt/test/com/google/gwt/sample/kitchensink/client/test/GwtTestCaseTest.java"/> </listAttribute> <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES"> <listEntry value="1"/> </listAttribute> <stringAttribute key="org.eclipse.jdt.junit.CONTAINER" value=""/> <booleanAttribute key="org.eclipse.jdt.junit.KEEPRUNNING_ATTR" value="false"/> <stringAttribute key="org.eclipse.jdt.junit.TESTNAME" value=""/> <stringAttribute key="org.eclipse.jdt.junit.TEST_KIND" value="org.eclipse.jdt.junit.loader.junit4"/> <listAttribute key="org.eclipse.jdt.launching.CLASSPATH"> <listEntry value="<?xml version="1.0" encoding="UTF-8"?> <runtimeClasspathEntry containerPath="org.eclipse.jdt.launching.JRE_CONTAINER" javaProject="com.hotye.gwt" path="1" type="4"/> "/> <listEntry value="<?xml version="1.0" encoding="UTF-8"?> <runtimeClasspathEntry id="org.eclipse.jdt.launching.classpathentry.defaultClasspath"> <memento exportedEntriesOnly="false" project="com.hotye.gwt"/> </runtimeClasspathEntry> "/> <listEntry value="<?xml version="1.0" encoding="UTF-8"?> <runtimeClasspathEntry internalArchive="/com.hotye.gwt/src" path="3" type="2"/> "/> <listEntry value="<?xml version="1.0" encoding="UTF-8"?> <runtimeClasspathEntry internalArchive="/com.hotye.gwt/test" path="3" type="2"/> "/> <listEntry value="<?xml version="1.0" encoding="UTF-8"?> <runtimeClasspathEntry internalArchive="/com.hotye.gwt/gwtext" path="3" type="2"/> "/> </listAttribute> <booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/> <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.google.gwt.sample.kitchensink.client.test.GwtTestCaseTest"/> <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="com.hotye.gwt"/> </launchConfiguration>
运行图