Running unit tests with changing dependency versions

This is a little how-to guide which explains you how you can run a set of unit test on configurations with different versions of the same plugin. This can be very useful when creating your own library which depends heavily on an actively developed dependency of which many versions exists and are being used (eg. Spring, Hibernate, OpenJPA, …)

Let’s assume that we have the following single-module maven project:

<groupId>be.pw.wordpress</groupId>
<artifactId>wordpress-demo</artifactId>
<version>1.0-SNAPSHOT</version>

The folder structure is very straightforward with the sources in src/main/java and the tests in src/test/java. To run the unit tests with different dependencies we will copy these tests, re-compile them and run them using the maven-shitty-plugin.

The first thing to do is to create a jar containing the test sources of the wordpress-demo module. This can be achieved by adding the maven-source-plugin to the wordpress-demo project.

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-source-plugin</artifactId>
	<executions>
		<execution>
			<id>test-sources-jar</id>
			<phase>verify</phase>
			<goals>
				<goal>test-jar</goal>
			</goals>
		</execution>
	</executions>
</plugin>

To keep things clear, the multi-version tests will be executed in a new project:

<groupId>be.pw.wordpress</groupId>
<artifactId>wordpress-demo-it</artifactId>
<version>1.0-SNAPSHOT</version>

In this pom you must only declare the maven-shitty-plugin:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>shitty-maven-plugin</artifactId>
	<version>1.0-alpha-3</version>
</plugin>

This was the easy part.

In the wordpress-demo-it project, create a folder src/it/test-v0_0_1 . Inside these folder you must create two files: a maven pom.xml and a goals.txt .
The pom.xml describes a virtual parent-less project:

<groupId>be.pw.wordpress</groupId>
<artifactId>wordpress-demo-it-0_0_1</artifactId>
<version>1.0-SNAPSHOT</version>

Adding the following plugin to this virtual project will add the sources of your unit tests to this project:

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<executions>
		<execution>
			<id>unpack-tests</id>
			<phase>generate-sources</phase>
			<goals>
				<goal>unpack</goal>
			</goals>
			<configuration>
				<artifactItems>
					<artifactItem>
						<groupId>be.pw.wordpress</groupId>
						<artifactId>wordpress-demo</artifactId>
						<version>1.0-SNAPSHOT</version>
						<classifier>test-sources</classifier>
					</artifactItem>
				</artifactItems>
				<outputDirectory>${project.build.testSourceDirectory}</outputDirectory>
			</configuration>
		</execution>
	</executions>
</plugin>

Now comes the tricky part: since this is a parent-less project, you must re-declare all your dependencies (and preferably with the test scope) and you must also declare the project you want test as a dependency.
So my dependencies might look like:

<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.11</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>com.h2database</groupId>
		<artifactId>h2</artifactId>
		<version>1.3.170</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.easytesting</groupId>
		<artifactId>fest-assert</artifactId>
		<version>1.4</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>be.pw.wordpress</groupId>
		<artifactId>wordpress-demo</artifactId>
		<version>1.0-SNAPSHOT</version>
		<scope>test</scope>
	</dependency>
</dependencies>

The last thing to do is the goals.txt

clean
test

After compiling the wordpress-demo and wordpress-demo-it project, go to the wordpress-demo-it project and run

mvn shitty:test

If everything goes right you’ll see something like:

[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'shitty'.
[INFO] ------------------------------------------------------------------------
[INFO] Building TestBuild
[INFO]    task-segment: [shitty:test]
[INFO] ------------------------------------------------------------------------
[INFO] [shitty:test {execution: default-cli}]
[INFO] Starting 1 test builds
[INFO] 
[INFO] -------------------------------------------------------------------------------
[INFO] 
[INFO] wordpress-demo-it-0_0_1 RUNNING
[INFO] wordpress-demo-it-0_0_1 SUCCESS (0:01:12.255) 
[INFO] 
[INFO] -------------------------------------------------------------------------------
[INFO] Test Summary (0:01:09.309)
[INFO]     Passed: 1
[INFO]     Failed: 0
[INFO] -------------------------------------------------------------------------------
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 minutes 10 seconds
[INFO] Finished at: Sun Dec 23 14:37:36 CET 2012
[INFO] Final Memory: 14M/150M

If not, go look in the src/it/wordpress-demo-it-0_0_1/build.log file or src/it/wordpress-demo-it-0_0_1/target/surefire-reports folder for more info.

You can create as many virtual project as you want, the shitty plugin will automatically pick them up (as long as they are in the it folder).

 

If you want a complete example you may want to look at my oqt-maven-plugin. I’m using a little bit of Java code to generate the integration test pom files, but this is the base I’m using for all my tests.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s