Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何修复java.lang.IllegalStateException:无法清除JavaAgentClassRegister。尚未调用Set方法。?_Spring Boot_Mockito_Junit4_Powermockito - Fatal编程技术网

Spring boot 如何修复java.lang.IllegalStateException:无法清除JavaAgentClassRegister。尚未调用Set方法。?

Spring boot 如何修复java.lang.IllegalStateException:无法清除JavaAgentClassRegister。尚未调用Set方法。?,spring-boot,mockito,junit4,powermockito,Spring Boot,Mockito,Junit4,Powermockito,我使用JunitRunner运行使用PowerMock和Mockito编写的单元测试 使用的是Spring启动版本 <version>2.0.5.RELEASE</version> 当我运行testcase时,我得到的错误如下所示 java.lang.IllegalStateException:无法清除JavaAgentClassRegister。尚未调用Set方法。 位于org.powermock.api.extension.agent.JavaAgentFramew

我使用JunitRunner运行使用PowerMock和Mockito编写的单元测试

使用的是Spring启动版本

<version>2.0.5.RELEASE</version>
当我运行testcase时,我得到的错误如下所示

java.lang.IllegalStateException:无法清除JavaAgentClassRegister。尚未调用Set方法。 位于org.powermock.api.extension.agent.JavaAgentFrameworkRegisterImpl.clear(JavaAgentFrameworkRegisterImpl.java:41) 位于org.powermock.modules.junit4.rule.PowerMockStatement.clearFrameworkAgentClassRegister(PowerMockRule.java:84) 位于org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:78) 位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 位于org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 位于org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 访问org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 位于org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 位于org.junit.runners.ParentRunner.run(ParentRunner.java:363) 位于org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) 位于org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) 位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) 位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) 位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) 位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

原因可能是什么?
如何做到这一点?

在JDK 8和PowerMock 2.0.2上运行测试时,我遇到了同样的问题,出现了以下一连串的异常。这只发生在JDK 8上,我尝试了JDK 9和11,它们工作得非常好。很明显这和ByteBuddy探员有关

Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
    Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@ff7da2b
        Failed to load MockMaker implementation: mock-maker-inline
            Internal problem occurred, please report it. Mockito is unable to load the default implementation of class that is a part of Mockito distribution. Failed to load interface org.mockito.plugins.MockMaker

                Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)

                Java               : 1.8
                JVM vendor name    : Oracle Corporation
                JVM vendor version : 25.171-b11
                JVM name           : Java HotSpot(TM) 64-Bit Server VM
                JVM version        : 1.8.0_171-b11
                JVM info           : mixed mode
                OS name            : Mac OS X
                OS version         : 10.15.2
                    Error during attachment using: net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@c746e76

您没有看到上述异常的原因是PowerMock规则的finally块抛出了一个异常,该异常掩盖了我上面显示的异常

 @Override
    public void evaluate() throws Throwable {
        Object annotationEnabler = loadAnnotationEnableIfPresent();
        try {
            injectMocksUsingAnnotationEnabler(target, annotationEnabler); //This line throws the exception I've shown above
            setFrameworkAgentClassRegister();
            fNext.evaluate();
        } finally {
            MockRepository.clear();
            clearMockFields(target, annotationEnabler);
            clearFrameworkAgentClassRegister();// This line throws another exception masking the first one
        }
    }

要查看上述异常,您需要远程调试运行测试的进程。在maven中,您可以使用
-Dmaven.surefire.debug
选项。

对不起,您有解决这个问题的方法吗(除了转到java 9+?)
@RunWith(JUnit4.class)
@PrepareForTest({SomeXService.class})
Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
    Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@ff7da2b
        Failed to load MockMaker implementation: mock-maker-inline
            Internal problem occurred, please report it. Mockito is unable to load the default implementation of class that is a part of Mockito distribution. Failed to load interface org.mockito.plugins.MockMaker

                Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)

                Java               : 1.8
                JVM vendor name    : Oracle Corporation
                JVM vendor version : 25.171-b11
                JVM name           : Java HotSpot(TM) 64-Bit Server VM
                JVM version        : 1.8.0_171-b11
                JVM info           : mixed mode
                OS name            : Mac OS X
                OS version         : 10.15.2
                    Error during attachment using: net.bytebuddy.agent.ByteBuddyAgent$AttachmentProvider$Compound@c746e76

 @Override
    public void evaluate() throws Throwable {
        Object annotationEnabler = loadAnnotationEnableIfPresent();
        try {
            injectMocksUsingAnnotationEnabler(target, annotationEnabler); //This line throws the exception I've shown above
            setFrameworkAgentClassRegister();
            fNext.evaluate();
        } finally {
            MockRepository.clear();
            clearMockFields(target, annotationEnabler);
            clearFrameworkAgentClassRegister();// This line throws another exception masking the first one
        }
    }