gpt4 book ai didi

java - 升级到1.43版本后无法运行JMockit测试

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:22 26 4
gpt4 key购买 nike

我有自己的项目,曾经与 Java 8 完美配合。我使用的非常基本的测试框架是 TestNG 和 JMockit。

最近我将 Java 版本升级到 Java 9,我注意到 JMockit 测试因 java 模块访问问题而失败。谷歌带我到这个堆栈溢出接受的解决方案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 世界发生了什么变化。

提前感谢您的选择。

最佳答案

也许@Rogério 的意思是查看 Release Notes ,其中从版本 1.42 开始需要使用 -javaagent JVM 参数。

尝试一下,Running Tests 上也提到了它部分

关于java - 升级到1.43版本后无法运行JMockit测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53005284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com