using java8 for unit testing while being backward compatible

16
Using java8 for unit testing while being backward compatible Nikola Petrov<[email protected]> Ontotext AD November 7, 2014 Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Upload: nikola-petrov

Post on 06-Aug-2015

62 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Using java8 for unit testing while being backward compatible

Using java8 for unit testing while being backwardcompatible

Nikola Petrov<[email protected]>

Ontotext AD

November 7, 2014

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 2: Using java8 for unit testing while being backward compatible

The problem

Most of us won’t be able to use/deploy JDK 8 in production for alooong time. But that shouldn’t stop us from using it, right?

It should be possible to sneak in JDK 8 in the back way, the sameway we snuck in Groovy and other libraries we wanted to use.

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 3: Using java8 for unit testing while being backward compatible

The problem

Most of us won’t be able to use/deploy JDK 8 in production for alooong time. But that shouldn’t stop us from using it, right?

It should be possible to sneak in JDK 8 in the back way, the sameway we snuck in Groovy and other libraries we wanted to use.

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 4: Using java8 for unit testing while being backward compatible

Maven

The Maven compiler plugin run in two separate lifecycles, compileand testCompile. Those can be configured separately.

The Maven Compiler even comes with support out of the box toseparate them.

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 5: Using java8 for unit testing while being backward compatible

Maven

The Maven compiler plugin run in two separate lifecycles, compileand testCompile. Those can be configured separately.

The Maven Compiler even comes with support out of the box toseparate them.

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 6: Using java8 for unit testing while being backward compatible

The easy way

<properties><maven.compiler.target>1.7</maven.compiler.target><maven.compiler.source>1.7</maven.compiler.source><maven.compiler.testTarget>1.8</maven.compiler.testTarget><maven.compiler.testSource>1.8</maven.compiler.testSource>

</properties>

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 7: Using java8 for unit testing while being backward compatible

The hard way...

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><executions>

<execution><id>default-compile</id><configuration>

<showDeprecation>true</showDeprecation><showWarnings>true</showWarnings><compilerArguments><source>${maven.compiler.target}</source><target>${maven.compiler.source}</target></compilerArguments>

</configuration></execution><execution>

<id>default-testCompile</id><configuration>

<showDeprecation>true</showDeprecation><showWarnings>true</showWarnings><compilerArguments><source>${maven.compiler.testTarget}</source><target>${maven.compiler.testSource}</target></compilerArguments>

</configuration></execution>

</executions></plugin>

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 8: Using java8 for unit testing while being backward compatible

Make sure that people use the right JDK

Some people in the team might try to build this with an olderversion

Enforce it in maven<plugin>

<groupId>org.apache.maven.plugins</groupId><artifactId>maven-enforcer-plugin</artifactId><version>1.3.1</version><executions>

<execution><id>enforce-java</id><goals>

<goal>enforce</goal></goals><configuration>

<rules><requireJavaVersion>

<version>${maven.compiler.testTarget}</version></requireJavaVersion>

</rules></configuration>

</execution></executions>

</plugin>

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 9: Using java8 for unit testing while being backward compatible

Make sure that people use the right JDK

Some people in the team might try to build this with an olderversion

Enforce it in maven

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-enforcer-plugin</artifactId><version>1.3.1</version><executions>

<execution><id>enforce-java</id><goals>

<goal>enforce</goal></goals><configuration>

<rules><requireJavaVersion>

<version>${maven.compiler.testTarget}</version></requireJavaVersion>

</rules></configuration>

</execution></executions>

</plugin>

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 10: Using java8 for unit testing while being backward compatible

Make sure that people use the right JDK

Some people in the team might try to build this with an olderversion

Enforce it in maven

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-enforcer-plugin</artifactId><version>1.3.1</version><executions>

<execution><id>enforce-java</id><goals>

<goal>enforce</goal></goals><configuration>

<rules><requireJavaVersion>

<version>${maven.compiler.testTarget}</version></requireJavaVersion>

</rules></configuration>

</execution></executions>

</plugin>

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 11: Using java8 for unit testing while being backward compatible

Make sure that people use the right JDK

Some people in the team might try to build this with an olderversion

Enforce it in maven<plugin>

<groupId>org.apache.maven.plugins</groupId><artifactId>maven-enforcer-plugin</artifactId><version>1.3.1</version><executions>

<execution><id>enforce-java</id><goals>

<goal>enforce</goal></goals><configuration>

<rules><requireJavaVersion>

<version>${maven.compiler.testTarget}</version></requireJavaVersion>

</rules></configuration>

</execution></executions>

</plugin>

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 12: Using java8 for unit testing while being backward compatible

Stop people from using java8 classes in the main codebase

Enforcing java version in production doesn’t mean that we are notallowed to use new methods or classes from JDK8

We want to prevent that from happening because we are going toget errors when we deploy

<plugin><groupId>org.codehaus.mojo</groupId><artifactId>animal-sniffer-maven-plugin</artifactId><version>1.7</version><executions>

<execution><id>signature-check</id><phase>verify</phase><goals>

<goal>check</goal></goals>

</execution></executions><configuration>

<signature><groupId>org.codehaus.mojo.signature</groupId><artifactId>java17</artifactId><version>1.0</version>

</signature></configuration>

</plugin>

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 13: Using java8 for unit testing while being backward compatible

Stop people from using java8 classes in the main codebase

Enforcing java version in production doesn’t mean that we are notallowed to use new methods or classes from JDK8

We want to prevent that from happening because we are going toget errors when we deploy

<plugin><groupId>org.codehaus.mojo</groupId><artifactId>animal-sniffer-maven-plugin</artifactId><version>1.7</version><executions>

<execution><id>signature-check</id><phase>verify</phase><goals>

<goal>check</goal></goals>

</execution></executions><configuration>

<signature><groupId>org.codehaus.mojo.signature</groupId><artifactId>java17</artifactId><version>1.0</version>

</signature></configuration>

</plugin>

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 14: Using java8 for unit testing while being backward compatible

Stop people from using java8 classes in the main codebase

Enforcing java version in production doesn’t mean that we are notallowed to use new methods or classes from JDK8

We want to prevent that from happening because we are going toget errors when we deploy

<plugin><groupId>org.codehaus.mojo</groupId><artifactId>animal-sniffer-maven-plugin</artifactId><version>1.7</version><executions>

<execution><id>signature-check</id><phase>verify</phase><goals>

<goal>check</goal></goals>

</execution></executions><configuration>

<signature><groupId>org.codehaus.mojo.signature</groupId><artifactId>java17</artifactId><version>1.0</version>

</signature></configuration>

</plugin>

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 15: Using java8 for unit testing while being backward compatible

Stop people from using java8 classes in the main codebase

Enforcing java version in production doesn’t mean that we are notallowed to use new methods or classes from JDK8

We want to prevent that from happening because we are going toget errors when we deploy

<plugin><groupId>org.codehaus.mojo</groupId><artifactId>animal-sniffer-maven-plugin</artifactId><version>1.7</version><executions>

<execution><id>signature-check</id><phase>verify</phase><goals>

<goal>check</goal></goals>

</execution></executions><configuration>

<signature><groupId>org.codehaus.mojo.signature</groupId><artifactId>java17</artifactId><version>1.0</version>

</signature></configuration>

</plugin>

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible

Page 16: Using java8 for unit testing while being backward compatible

We are ready

Classes in src/main/java are allowed to be written in java7/java6only

Classes in src/test/java can contain Java8 code

Nikola Petrov<[email protected]> Using java8 for unit testing while being backward compatible