(PS,因为在GWT中建立JUNIT测试做了很久,所以先写点这个。)
在GWT中使用JUNIT,以GWT自带的Hello工程为例
1.引入Hello工程到Eclipse中,即将Hello解压到EC工程目录下,然后在EC中新建一个叫Hello的GWT工程。
2.建立一个叫test的代码目录,然后建立一个com.google.gwt.sample.hello.client包,因为我们测试的是com.google.gwt.sample.hello.Hello这个MODEL的CLIENT端的东西,所以一定要建这个包,而且名称还一定要这么叫,与该MODEL下CLIENT端的包名一样。
3.建立TestHello的测试类,记住继承的是GWTTest类,代码如下:
public class TestHello extends GWTTestCase ...{
/**//*
* Specifies a module to use when running this test case. The returned
* module must cause the source for this class to be included.
*
* @see com.google.gwt.junit.client.GWTTestCase#getModuleName()
*/
public String getModuleName() ...{
return "com.google.gwt.sample.hello.Hello";
}
public void testStuff() ...{
assertTrue(2 + 2 == 4);
}
}
4.运行测试前要确认,你的工程引用了gwt-dev-windows.jar 和gwt-user.jar ,(实际上,后者是默认引用的,前都要自己手动载入。),以后在JUNIT运行配置中的classpath选项上加上工程的test和src代码目录。就可以运行通过了。