简体   繁体   中英

In Intellij Idea, how 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 throws 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

Running tests via another Java interpreter in Maven project is not supported in IntelliJ IDEA yet. Here is the feature request for that:

https://youtrack.jetbrains.com/issue/IDEA-85478

Feel free to vote and follow it for updates.

There is a workaround described by one of the users (separate Maven profile): https://youtrack.jetbrains.com/issue/IDEA-85478#focus=Comments-27-1994481.0-0

By the way, this is supported in Gradle<->IntelliJ IDEA integration:

compileTestJava {
    sourceCompatibility = "1.8"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM