初步使用GWTTestcase的单元测试

采用GWT提供的单元测试类,可以在不启动host模式的情况下测试客户端代码.对指定的模块测试其客户端的所有代码.

(详细请参考:http://code.google.com/docreader/#p=google-web-toolkit-doc-1-5&s=google-web-toolkit-doc-1-5&t=DevGuideJUnitCreation)

 

 

在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:

  1. 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.
  2. 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.
  3. 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.
  4. Compile your test case class to bytecode. You can use the Java compiler directly using javac or a Java IDE such as Eclipse.
  5. 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>

 运行图

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值