初步使用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>

 運行圖

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章