在具有surefire和failsafe的预览功能的情况下运行测试时出现问题 [英] Problem running tests with enabled preview features in surefire and failsafe

查看:130
本文介绍了在具有surefire和failsafe的预览功能的情况下运行测试时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用--enable-preview将项目迁移到Java 12.

I'm trying to migrate a project to Java 12, with --enable-preview.

我在编译器设置中添加了--enable-preview:

I added --enable-preview in compiler settings:

        <plugin>                                                            
            <artifactId>maven-compiler-plugin</artifactId>                  
            <version>3.8.0</version>                                        
            <configuration>                                                 
                <release>12</release>                          
                <compilerArgs>                                                                                  
                    <arg>--enable-preview</arg>                             
                </compilerArgs>                                                                      
            </configuration>                                                
        </plugin>                                                                                                                                         

并且还将其添加到argLine中以确保安全和失效保护:

And also added it in argLine for surefire and failsafe:

<properties>                                                                                             
    <argLine>--enable-preview</argLine>                        
</properties> 

并执行mvn clean verify结果:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M3:test (default-test) on project lombok-jdk10: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:3.0.0-M3:test failed: java.lang.UnsupportedClassVersionError: Preview features are not enabled for com/kirela/lombok/BarTest (class file version 56.65535). Try running with '--enable-preview' -> [Help 1]

我还尝试将argLine直接添加到surefire/failsafe配置中,但是结果是相同的.

I also tried adding argLine directly to surefire/failsafe configuration, but the result is same.

我在这里想念什么?

这是surefire/failsafe中的错误吗?

I this a bug in surefire/failsafe?

Surefire和故障安全配置:

Surefire and failsafe config:

        <plugin>                                                            
            <groupId>org.apache.maven.plugins</groupId>                     
            <artifactId>maven-surefire-plugin</artifactId>                  
            <version>3.0.0-M3</version>                                     
            <configuration>                                                 
                <forkCount>2</forkCount>                                    
            </configuration>                                                
        </plugin>                                                           
        <plugin>                                                            
            <groupId>org.apache.maven.plugins</groupId>                     
            <artifactId>maven-failsafe-plugin</artifactId>                  
            <version>3.0.0-M3</version>                                     
            <executions>                                                    
                <execution>                                                 
                    <goals>                                                 
                        <goal>integration-test</goal>                       
                        <goal>verify</goal>                                 
                    </goals>                                                
                </execution>                                                
            </executions>                                                   
            <configuration>                                                 
                <forkCount>2</forkCount>                                    
            </configuration>                                                                                                                              
        </plugin> 

最小的工作示例在这里: https://github.com/krzyk/lombok-jdk10-example

Minimal working example is here: https://github.com/krzyk/lombok-jdk10-example

该项目失败并显示--enable-preview,但是当我删除它时可以正常工作.

The project fails with --enable-preview, but works when I remove it.

推荐答案

有两种解决方案:

--enable-preview添加到MAVEN_OPTS环境变量.

Add --enable-preview to MAVEN_OPTS environment variable.

argLine可以做的事情没有任何问题. 该插件运行JUnit过滤器,该过滤器最终选择相关的类以在一个或多个JVM中运行. 因此,JUnit引擎运行两次.一次在插件JVM中,第二次在分叉JVM中.

The argLine does what it has to do without any issue. The plugin runs JUnit filter which finally selects relevant classes to run in one or multiple JVMs. So the JUnit engine runs twice. Once in plugin JVM, and second in the forked JVM.

由于类使用与Maven中Java运行时支持的版本不同的主要或次要版本(* .class文件的字节码)编译,因此此JRE失败,因为Maven中的Java无法理解字节码.因此,奇怪的是,同一JVM(javac)根据JVM选项产生了两个主要版本,而来自同一JVM的java不理解它本身是不兼容的.尽管分叉JVM中的版本完全可以并且可以理解javac编译的类,因为javac和分叉JVM以相同的选项--enable-preview开头. 就像您使用maven-compiler-plugin使用工具链使用Java 12编译源代码并使用Java 11运行整个Maven构建一样.因此,将使用比JRE更高的版本(字节码)编译类.了解Maven流程.

Due to the classes are compiled with different major or minor version (in bytecode of *.class files) than the version of Java runtime supports in Maven, this JRE fails because Java in Maven does not understand the bytecode. So, it is curious that the same JVM (javac) produced two major versions depending on JVM option and java from the same JVM does not understand it been incompatible for itself. Although version in forked JVM is totally fine and understands the the classes compiled by javac because javac and forked JVM start with the same option --enable-preview. It is the same situation as if you compiled your sources with Java 12 by maven-compiler-plugin using the toolchain and run the whole Maven build with Java 11. So the classes would be compiled with higher version (in bytecode) than the JRE could understand in Maven process.

我们希望对提供者进行返工并在派生的JVM中执行过滤,但这是非常复杂的更改,仍然存在疑问.

We have a wish to rework providers and perform the filtering inside of the forked JVM but this is very compilicated change and still questionary.

问题是我使用了forkCount,看来surefire没有将参数传递给在fork中运行的JVM.

The issue is that I used forkCount, it appears surefire doesn't pass parameters to JVM run in fork.

从surefire/failsafe配置中删除forkCount参数.

Remove the forkCount parameter from surefire/failsafe configuration.

这当然会导致测试在单个JVM中运行,因此,如果您想使用fork来加速测试,那么它将无法正常工作.

This will of course cause the tests to run in a single JVM, so if you wanted to speed up the tests using the forks, it won't work now.

这篇关于在具有surefire和failsafe的预览功能的情况下运行测试时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆