Wednesday, March 4, 2009

Maven2-Groovy-JUnit-Guice

Here's how I'm currently setting up my testing with groovy and maven, using Guice and JUnit. (I had trouble getting this all to work with TestNG, which is my preferred testing framework, so if someone gets it all working, please let me know.)

I'm usting AtUnit to help out with the Guice/JUnit integration. It makes things very simple.

In your pom.xml build plugins section:


<plugin>
<groupid>org.codehaus.groovy.maven</groupid>
<artifactid>gmaven-plugin</artifactid>
<version>1.0-rc-4</version>
<executions>
<execution>
<goals>
<goal>generateStubs</goal>
<goal>compile</goal>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>


My groovy tests are located in src/test/groovy (Using Eclipse I then mark this as a source folder also so it's easier to use in a flattened package structure.)

When you look at the AtUnit examples, you'll see several ways to set things up. I like to use my existing Guice module class that I've already created, so I start out with a TestBase class that my tests will extend...


public class TestBase implements Module {

public void configure(Binder binder) {
binder.install(new MyGuiceModule());
}
}


If you don't want to use a Module, you can just do all your binding right in the configure method above and everything will work just fine (and this is the way they demonstrate on the AtUnit site example.)

Now one of my groovy tests:


@RunWith(AtUnit.class)
@Container(Container.Option.GUICE)
public class ExecutionLogTest extends TestBase {

@Inject @Unit ExecutionLogs execLogs;

@Test
public void testExeuctionLogs() {
println "testExeuctionLogs"
//... execLogs.find(... )
}
}


mvn clean install and that's it.

No comments :

Post a Comment

Blogger Syntax Highliter