我有一个自己的项目,该项目以前可以与Java 8完美配合。我使用的非常基本的测试框架是TestNG和JMockit。
最近,我将Java版本升级到Java 9,并且发现JMockit测试失败,并出现Java模块访问问题。 Google带我去了这个接受堆栈溢出的解决方案module java.base does not read module java.desktop,建议将JMockit升级到1.34或更高版本。之后,我将其升级到了当前的最新版本1.43。
完成此升级后,我不再可以运行测试,而我得到的只是控制台输出中的NullPointerException。我的重现该问题的测试如下:
package com.my.org;
import mockit.FullVerifications;
import mockit.Injectable;
import mockit.Tested;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class JMockitIssueTest {
private class Delegate {
public void doSomething() {
System.out.println("Doing something");
}
}
private class ClassUnderTest {
private final Delegate delegate;
private ClassUnderTest(Delegate delegate) {
this.delegate = delegate;
}
public void useDelegate() {
delegate.doSomething();
}
}
@BeforeMethod
public void setUp() throws Exception {
}
@Injectable
private Delegate delegate;
@Tested
private ClassUnderTest classUnderTest;
@Test
public void itShouldUseDelegate() {
classUnderTest.useDelegate();
new FullVerifications() {{
delegate.doSomething();
}};
}
}
最初没有setUp方法,但是在添加一个空方法之后,我在控制台输出中获得了更多信息,我在下面列出了这些信息:
Test ignored.
java.lang.NullPointerException
at mockit.integration.testng.TestNGRunnerDecorator.beforeInvocation(TestNGRunnerDecorator.java:32)
at org.testng.internal.invokers.InvokedMethodListenerInvoker$InvokeBeforeInvocationWithoutContextStrategy.callMethod(InvokedMethodListenerInvoker.java:84)
at org.testng.internal.invokers.InvokedMethodListenerInvoker.invokeListener(InvokedMethodListenerInvoker.java:62)
at org.testng.internal.Invoker.runInvokedMethodListeners(Invoker.java:493)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:533)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
我尝试了多种排列方式,例如使用@Mocked Delegate
实例和自己创建ClassUnderTest
甚至回滚到Java 8。
我什至尝试将测试移至jUnit(因为有1400多个测试类,所以这并不是一个理想的选择)。目前,我的想法不多了,但另一方面,上述用例是如此基础,以至于我真的希望我做的是一件愚蠢的事情,因为我使用的是此模拟的1.27版本,所以我不知道JMockit世界中发生了什么变化框架。
预先感谢您的选择。
答案 0 :(得分:0)
也许@Rogério表示要查看Release Notes,从1.42版开始,需要使用-javaagent JVM参数。
尝试一下,在Running Tests部分中也提到了