问题描述
有一个由Maven管理的简单模块化java项目。
我正在尝试使用 mvn test
运行 JUnit 5 测试,考虑到 JUnit 5 不要求测试类和方法公开才能工作 (docs)。
不幸的是,只有当测试类和测试方法都是公共的时,测试才能按预期运行。
我想 Surefire 插件允许使用 JVM 参数或任何其他方式以某种方式修复它,但找不到应该如何完成的线索。
如何配置我的 Maven 构建以在不公开所有测试的情况下运行测试?
环境:
Maven 3.6.3
Java 11
JUnit 5.7.1
Surefire plugin 3.0.0-M5
module-info.java
module my.pkg {
exports my.pkg;
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>a</groupId>
<artifactId>a</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<properties>
<version.java>11</version.java>
<version.junit>5.7.1</version.junit>
<version.plugin.maven.compiler>3.8.1</version.plugin.maven.compiler>
<version.plugin.maven.surefire>3.0.0-M5</version.plugin.maven.surefire>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${version.plugin.maven.compiler}</version>
<configuration>
<release>${version.java}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.plugin.maven.surefire}</version>
</plugin>
</plugins>
</build>
</project>
尽管如此,调用 mvn test
会导致 MyTest.java
失败并出现异常。
另一方面,使类和测试方法都 public
使测试通过。
例外:
java.lang.reflect.InaccessibleObjectException: Unable to make my.pkg.MyTestA() accessible:
module my.module does not "opens my.pkg" to unnamed module @2b48a640
MyTest .java
class MyTest {
@Test
void shouldDo() {
assertEquals(1,1);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)