英文:
In Intellij Idea, how to run tests with different java version than my source java version
问题
我的主要源代码使用Java版本7。
我想在我的测试中使用Java 8,所以我更新了编译器插件如下:
<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>
这样我就可以在单元测试中使用Java 8的特性。当我从命令行使用Maven运行测试时,这是有效的。
但是当我直接从IntelliJ运行测试时,它会抛出错误,说“lambda表达式在源版本7中不受支持”。
看起来它在测试中使用了相同的源版本。
我如何配置IntelliJ以在测试中使用不同的Java版本?
英文:
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
答案1
得分: 1
在 IntelliJ IDEA 中尚不支持在 Maven 项目中通过另一个 Java 解释器运行测试。以下是该功能的请求链接:
https://youtrack.jetbrains.com/issue/IDEA-85478
欢迎投票并关注以获取更新。
其中一位用户提供了一种解决方法(使用单独的 Maven 配置文件):https://youtrack.jetbrains.com/issue/IDEA-85478#focus=Comments-27-1994481.0-0
顺便提一下,在 Gradle <-> IntelliJ IDEA 集成中是支持的:
compileTestJava {
sourceCompatibility = "1.8"
}
英文:
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"
}
答案2
得分: 0
在Maven项目中使用不同的JDK版本运行测试自从IntelliJ IDEA 2022.1版本开始可用。
英文:
Running tests with a different JDK version in Maven project is available since IntelliJ IDEA 2022.1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论