【问题标题】:How to write Junit with Java 1.7 while running base code with 1.6如何在使用 1.6 运行基本代码时使用 Java 1.7 编写 Junit
【发布时间】:2013-10-13 18:05:20
【问题描述】:

我想在我的测试中使用 java 7 并在代码中使用 java 6。如何做到这一点?

【问题讨论】:

  • 这是一个非常糟糕的主意,恕我直言。
  • 如果您对环境有任何控制权,那么让它们都使用相同的 JDK 版本是有意义的。
  • 我没有任何控制权。在代码上我依赖于 java 1.6,在测试中我想使用更新的语法。

标签: java junit version-compatibility


【解决方案1】:

还可以在maven-compiler-plugin的执行阶段testCompile设置源和目标。所以尝试这样的事情

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <executions>
       <execution>
           <id>testCompileWithJDK7</id>
           <phase>test-compile</phase>
           <configuration>
              <source>1.7</source>
              <target>1.7</target>              
           </configuration> 
       </execution>
    </executions>
</plugin>

【讨论】:

    【解决方案2】:

    您可以将 maven-surefire-plugin 使用的 jvm 设置为指向 java 7 可执行文件。

    http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#jvm

    【讨论】:

    • Maven 失败:org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project MyProject: Compilation failure
    • 你应该将maven-compiler-plugin的source和target属性设置为1.6,并确保mvn是使用jdk 6启动的。看来你的源代码与jdk版本不兼容你开始了 mvn 构建。您能否更新问题并添加:jdk mvn is started with, maven-compiler-plugin config, maven-surefire-plugin config
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    相关资源
    最近更新 更多