Unit Testing with different JDK

This could be really useful, if for instance the production platform is Java 1.4 (not generics, autoboxing and lots of other nice features 😦 ) but you do want to use Junit 4 anyway.

The Maven compiler plugin allows to set different source for compiling the application and the unit tests. In this way the brill features of Junit 4 are available even if you are stuck to the ancient 1.4.

Here is how to configure the pom:

<!-- set source for compiling (1.4) and testing (1.5) -->
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>RELEASE</version>
                <configuration>
                    <source>1.4</source>
                    <target>1.4</target>
                </configuration>
                <executions>
                    <execution>
                        <id>compile-tests</id>
                        <phase>process-test-sources</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <source>1.5</source>
                            <target>1.5</target>
                            <compilerArguments>
                                <bootclasspath>
                                    ${JAVA_HOME}/jre/lib/rt.jar
                                </bootclasspath>
                            </compilerArguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

The <bootclasspath> tag will need to point to the Java Runtime rt.jar (all Java built-in classes).

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

%d bloggers like this: