与 JUNIT5 一起使用时在 JMockit 库中获取异常

Getting Exception within JMockit Library when using with JUNIT5

我正在尝试将所有 JUNIT4 测试用例转换为使用 Jmockit 的 JUNIT5 测试用例。当我设法转换其中之一并执行时,我在 JMockit 库中收到此错误,这是我的应用程序的依赖项,

java.lang.NullPointerException
    at mockit.internal.startup.Startup.retransformClass(Startup.java:98)
    at mockit.internal.state.CachedClassfiles.getClassfile(CachedClassfiles.java:123)
    at mockit.internal.ClassFile.createReaderOrGetFromCache(ClassFile.java:92)
    at mockit.internal.classGeneration.ImplementationClass.generateClass(ImplementationClass.java:35)
    at mockit.internal.expectations.mocking.BaseTypeRedefinition.generateNewMockImplementationClassForInterface(BaseTypeRedefinition.java:163)
    at mockit.internal.expectations.mocking.BaseTypeRedefinition.createMockInterfaceImplementationDirectly(BaseTypeRedefinition.java:124)
    at mockit.internal.expectations.mocking.BaseTypeRedefinition.createMockedInterfaceImplementationAndInstanceFactory(BaseTypeRedefinition.java:84)
    at mockit.internal.expectations.mocking.BaseTypeRedefinition.redefineType(BaseTypeRedefinition.java:61)
    at mockit.internal.expectations.mocking.TypeRedefinition.redefineType(TypeRedefinition.java:28)
    at mockit.internal.expectations.mocking.FieldTypeRedefinitions.redefineFieldType(FieldTypeRedefinitions.java:78)
    at mockit.internal.expectations.mocking.FieldTypeRedefinitions.redefineFieldType(FieldTypeRedefinitions.java:65)
    at mockit.internal.expectations.mocking.FieldTypeRedefinitions.redefineFieldTypes(FieldTypeRedefinitions.java:53)
    at mockit.internal.expectations.mocking.FieldTypeRedefinitions.<init>(FieldTypeRedefinitions.java:33)
    at mockit.integration.TestRunnerDecorator.handleMockFieldsForWholeTestClass(TestRunnerDecorator.java:142)
    at mockit.integration.junit5.JMockitExtension.postProcessTestInstance(JMockitExtension.java:52)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$invokeTestInstancePostProcessors(ClassTestDescriptor.java:349)
    at org.junit.jupiter.engine.descriptor.JupiterTestDescriptor.executeAndMaskThrowable(JupiterTestDescriptor.java:215)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$invokeTestInstancePostProcessors(ClassTestDescriptor.java:349)
    at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:193)
    at java.util.stream.ReferencePipeline.accept(ReferencePipeline.java:175)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312)
    at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:743)
    at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:742)
    at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.invokeTestInstancePostProcessors(ClassTestDescriptor.java:348)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.instantiateAndPostProcessTestInstance(ClassTestDescriptor.java:270)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$testInstanceProvider(ClassTestDescriptor.java:259)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$testInstanceProvider(ClassTestDescriptor.java:263)
    at java.util.Optional.orElseGet(Optional.java:267)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$testInstanceProvider(ClassTestDescriptor.java:262)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:82)
    at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:59)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare[=10=](NodeTestTask.java:83)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:83)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:69)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:112)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively(NodeTestTask.java:112)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

请在此处找到可用于调试的示例代码,

https://github.com/siva54/jmockit-junit5-Whosebug/blob/master/test/java8testing/JUnit5Test.java

JMockit 版本:1.47

木星 JUNIT 版本:5.3.2

IntelliJ 版本:2019.1 旗舰版

谁能帮忙。

发现问题。该问题与使用的 Jmockit 版本有关。在我将 JMockit 版本恢复到 1.42 后问题得到解决,

<dependency>
  <groupId>org.jmockit</groupId>
  <artifactId>jmockit</artifactId>
  <version>1.42</version>
  <scope>test</scope>
</dependency>

该问题与构建可用于改造 类 以模拟对象的工具密切相关。对于 JMockit 运行,Jmockit JAR 需要作为 java 代理传递。在 1.43 之前,如果没有传递 java 代理,JMockit 库会搜索项目的依赖项并将其自动添加到 VM 对象中。在 1.42 之后,JMockit 开发人员认为这种查找 JAR 的方式不是一个好的标准,因此被完全删除。因此需要手动传递 java 代理。

这里有一些与该问题相关的参考链接,

https://github.com/jmockit/jmockit1/issues/540#issuecomment-444316617

https://github.com/jmockit/jmockit1/issues/546

https://github.com/jmockit/jmockit1/issues/568

解决此类问题的方法是

  • 移至版本 1.42
  • 遵循 JMockit 开发人员的这个示例并执行相同的操作。

https://github.com/jmockit/jmockit1/blob/master/samples/petclinic/pom.xml