【发布时间】:2018-09-27 15:50:17
【问题描述】:
我有一个具有以下结构的项目
src
|_ main
| |_ java
| |_ com.company.product
| |_ packageA
| |_ packageB
|_ test
|_ java
|_ com.company.product
|_ packageA
|_ packageB
运行mvn test 时,我在packageA 中的测试通过,而在packageB 中的测试失败。运行mvn test -Dtest="com.company.product.packageB.**" 时,packageB 中的测试通过。此外,运行 mvn test -Dtest="com.company.product.**" 也会使 packageB 测试失败,但 packageA 测试不会失败。 为什么mvn test 没有通过所有应该通过的测试?
packageB 中的测试详情:
@Test
void createNew() {
String user = "testUser";
//This calls a third party API that is throwing a
//InvocationTargetException when running packages together
Connection connect = new Connection(user);
String resultText = connect.getResultText();
assertNotNUll(connect);
assert (resultText).equals("Process Complete");
}
运行第三方 API 调用所需的 jar 包含在 pom 中,如下所示。
<dependency>
<groupId>com.third.party.api</groupId>
<artifactId>third-party-api</artifactId>
<version>1.0.0</version>
</dependency>
使用 Java 1.8.0_74 和 Maven 3.5.4。
编辑: Maven 返回的错误:
createNew() Time elapsed: 0.001 sec <<< ERROR!
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.company.product.packageB.MyTest.createNew(MyTest.java:11)
Caused by: java.lang.reflect.InvocationTargetException
at com.company.product.packageB.MyTest.createNew(MyTest.java:11)
Caused by: java.lang.RuntimeException: Error when creating RpcClientStub. Cause : java.lang.NoClassDefFoundError: Could not i
nitialize class com.third.party.apitransport.session.ArRpcCallContext
at com.company.product.packageB.MyTest.createNew(MyTest.java:11)
...
Results :
Tests in error:
MyTest.createNew:11 » Runtime java.lang.reflect.InvocationTargetEx...
MyTest.createAndUpdate:29 » Runtime java.lang.reflect.Invocation...
MyTest.connect:51 » Runtime java.lang.reflect.InvocationTarget...
Tests run: 9, Failures: 0, Errors: 3, Skipped: 0
编辑: 正如 Ivan 在 cmets 中指出的那样,解决方法是添加清理。
private static String systemOsName;
@BeforeAll
public static void setOsName(){
systemOsName = System.getProperty("os.name");
}
...
@AfterAll
public static void cleanup(){
Constants.setFilePathSeparator("");
System.setProperty("os.name",systemOsName);
}
【问题讨论】:
-
您应该发布 Maven 返回的完整错误。
-
大概他们在争夺一些共享资源。
-
我在 MyTest.java 中有 3 个测试,并且将所有 3 个测试一起运行通过。您的意思是这些软件包正在争夺共享资源吗? PackageA没有使用第三方api,就是在报错的那一行,供参考。
-
看起来
packageA中的测试在完成后不会执行清理(关闭资源/连接、删除创建的文件、回滚数据库更改)。由于packageB中的测试无法初始化这些资源 -
不确定需要清理哪些资源。 packageA 测试非常基础。以下是最复杂的:gist.github.com/JohnSBarden/1475872f2feb4133c7d1e9d544e4f1f8
标签: java maven intellij-idea junit