I want to run tests with different java version than my source java version

已回答

My main source has java version 7

I want to use java 8 from my tests. so i updated the compiler plugin to

 

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>7</source>
<target>7</target>
<testSource>8</testSource>
<testTarget>8</testTarget>
</configuration>
</plugin>

This way I can use java 8 features in unit tests. This works fine when i run the tests using maven from commandline.

 

But when i run the test directly from intellij, it throughs error saying 'lambda expressions are not supported in source 7' 

looks like it use the same source version for tests. 

 

How do i configure Intellij to use different java version for tests

1

Hello,

Unfortunately, it is not supported at the moment.

We hope to add support for this configuration in one of the future updates. Related feature request on our issue tracker: IDEA-85478. Feel free to follow it for updates.

A workaround that you can try:

There's a workaround to force Idea to use the language level defined in <testSource> when importing Maven project. You can create a separate Maven profile only for Idea with different compiler plugin settings:

    <profiles>

<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<testSource>1.8</testSource>
<testTarget>1.8</testTarget>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<profile>
<id>ide</id>
<activation>
<activeByDefault>false</activeByDefault>
<property>
<name>idea.maven.embedder.version</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<testSource>1.8</testSource>
<testTarget>1.8</testTarget>
</configuration>
</plugin>
</plugins>
</build>
</profile>

</profile>

It will unblock developers of libraries who need to target JRE 1.6 but want to use 1.8 in unit tests.

0

请先登录再写评论。