using java8 for unit testing while being backward compatible
TRANSCRIPT
![Page 1: Using java8 for unit testing while being backward compatible](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022071723/55c32552bb61ebce128b4603/html5/thumbnails/16.jpg)
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