Friday, July 06, 2007

Creating a GwtTestCase under eclipse

Creating a new JUnit test case with GWT integration - and having it work with Eclipse.

Perhaps you have a project structure that's different than what the GWT tools provide for you automatically? Perhaps you like to know what's going on under the hood? Either way, these steps should illuminate a little bit about how GWT wants you to invoke its GWTTestCase tests.

Steps:
- Create a test folder, and mirror the "client" package name you have under src. Eg: src/com/goodstuff/client/ would have an associated test/com/goodstuff/client/ directory for tests. The tests will not run successfully if you use a different package name.

- Create a test case within this package. Have it extend from the com.google.gwt.junit.client.GWTTestCase class.

- Implement the getModuleName() method. More than likely this will be the same for all your tests. This will be the Module Name of your main application. For me, it was "davefoolery.gwtTest.TestTwoApp". A 'module' has a corresponding file ending in .gwt.xml, so you'll know you have the right module name if the module com.yourcompany.FancyApp has an associated com/yourcompany/FancyApp.gwt.xml file.

- Create a run configuration for this test - easiest way to do that is to go run->run as JUnit Test. This will fail horribly, don't worry - we're not done yet.

- Add the gwt-dev-*.jar to the classpath of the run configuration for this test. If you want (not recommended), you could also add it to the project wide configuration. * See note at end.

- Ensure both "src" and "test" sub directories are in the classpath configuration for this test - GWT needs these to find the various files we're pointing at (test .java files, etc).

- Ensure you don't use any Java 5 features in your GWTTestCase tests.

- Run your test - it should pass now.


*NOTE: The -dev*.jar files use different names under Linux/OSX/Windows, and so will cause your runtime configurations/project files to be unportable if a different platform author needs to import your .project/.classpath/... settings.

8 comments:

vjrj said...

I read in your gwt group post that you are also using maven2. How do you include the gwt-dev-*.jar with maven? I get the same error with 'mvn test':

java.lang.NoClassDefFoundError: com.google.gwt.dev.GWTShell

TIA

Marco Bizzarri said...

Concerning the protability of linux/windows jar: you can define it as an Eclipse variable, and then leave it to be defined by the developer (?)

Dave LeBlanc said...

Marco:

Good point - using a variable would retain project portability.


Vicente:

Hmmn - you're right - this is most certainly an error. Looks like we need to pester the developer of the GWT plugin for Maven2.

Hmmn, looking further, it seems as if gwt-linux, gwt-mac, gwt-windows are all stored on the remote repository. This post gives me hope: http://mail-archive.objectweb.org/xwiki-commits/2007-06/msg00740.html

I'll be looking into this today, and mail you if I find anything.

vjrj said...

Thanks for your comments Marco and Dave.

Today I was trying this in my pom.xml:

<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>${google.webtoolkit.devjar}</artifactId>
<version>1.4.10</version>
<scope>system</scope>
<systemPath>${google.webtoolkit.home}/${google.webtoolkit.devjar}.jar</systemPath>
</dependency>

and in my settings.xml:

<google.webtoolkit.devjar>gwt-dev-linux</google.webtoolkit.devjar>

(but I have other unrelated problems to solve with my test)

I see yesterday a old post in the maven-gwt group without any solution:
http://groups.google.com/group/gwt-maven/browse_thread/thread/31515fad628e4a23/d75b8fc9e0cf4db8?lnk=st&q=testcase+gwt+maven&rnum=1#d75b8fc9e0cf4db8

BR,


Vicente

Anonymous said...

Thanks! I have things separated out and pull from a ui, shared, and common-ui, common-shared projects, and didn't have all of the sourcecode folders for each one on the test classpath. Adding them makes everything work like butter. Thank you for the post. - Cobbie

Anonymous said...

Hi

I have a test class that extends from GWTTestCase. Whenever I run the testcase, I used to get this error , "[ERROR] Unable to find 'com/mycompany/myclient/client/store.gwt.xml' on your classpath; could be a typo, or maybe you forgot to include a classpath entry for source?". So I changed the path of store.gwt.xml to com/mycompany/myclient/client folder. Now I am getting the error: "com.google.gwt.junit.JUnitFatalLaunchException: The test class 'com.mycompany.myclient.client.StoreTest' was not found in module 'com.mycompany.myclient.client.store'; no compilation unit for that type was seen
at com.google.gwt.junit.JUnitShell.checkTestClassInCurrentModule(JUnitShell.java:193)
at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:628)
at com.google.gwt.junit.JUnitShell.runTest(JUnitShell.java:150)
at com.google.gwt.junit.client.GWTTestCase.runTest(GWTTestCase.java:219)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at com.google.gwt.junit.client.GWTTestCase.run(GWTTestCase.java:132)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

Could you please help me in this?

Thanks
Vinodhini
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

"

Anonymous said...

You need to have the same "package hierarchy" for your test classes as your regular classes. The article mentions having one under src and one under test, but I keep them in separate projects, so they have the same exact hierarchy, but one is MyProject and one is MyProjectTests.

Dennys said...

After doing this program i cud run this test class. but my web application project is not running. it shows Unknown argument: -style.