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

时间:2019-07-10 15:31:51

标签: java junit junit5 jmockit

我正在尝试将所有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$5(ClassTestDescriptor.java:349)
    at org.junit.jupiter.engine.descriptor.JupiterTestDescriptor.executeAndMaskThrowable(JupiterTestDescriptor.java:215)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$invokeTestInstancePostProcessors$6(ClassTestDescriptor.java:349)
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
    at java.util.stream.ReferencePipeline$2$1.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$2(ClassTestDescriptor.java:259)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$testInstanceProvider$3(ClassTestDescriptor.java:263)
    at java.util.Optional.orElseGet(Optional.java:267)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$testInstanceProvider$4(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$0(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$4(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$4(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-stackoverflow/blob/master/test/java8testing/JUnit5Test.java

JMockit版本:1.47

木星JUNIT版本:5.3.2

IntelliJ版本:2019.1 Ultimate

任何人都可以帮忙。

1 个答案:

答案 0 :(得分:2)

发现了问题。该问题与所使用的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