How to fix java.lang.IllegalStateException: Cannot clear JavaAgentClassRegister. Set method has not been called.?

£可爱£侵袭症+ 提交于 2020-03-22 09:12:09

问题


I am using JunitRunner for running unit tests written using PowerMock and Mockito .

Spring Boot Version used is

<version>2.0.5.RELEASE</version>

pom.xml has below dependencies

     <dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>

</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-javaagent</artifactId>
<version>2.0.0-beta.5</version>
</dependency>

The test class has annotations shown below

@RunWith(JUnit4.class)
@PrepareForTest({SomeXService.class})

When I run the testcase, I am getting error shown as below

java.lang.IllegalStateException: Cannot clear JavaAgentClassRegister. Set method has not been called. at org.powermock.api.extension.agent.JavaAgentFrameworkRegisterImpl.clear(JavaAgentFrameworkRegisterImpl.java:41) at org.powermock.modules.junit4.rule.PowerMockStatement.clearFrameworkAgentClassRegister(PowerMockRule.java:84) at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:78) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

What Could be the reason? How to make this work?


回答1:


I had the same problem when running my tests on JDK 8 and PowerMock 2.0.2 and got the following chain of exceptions. It only happens with JDK 8 I tried JDK 9 and 11 and they work perfectly fine. Apparently it has something to do with ByteBuddy agent.

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

The reason you don't see the above exceptions is PowerMock rule's finally block throws an exception that masks the exception I've shown above.

 @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
        }
    }

To see the above exceptions you need to remote debug the process that runs your tests. In maven you can use -Dmaven.surefire.debug options.



来源:https://stackoverflow.com/questions/59468657/how-to-fix-java-lang-illegalstateexception-cannot-clear-javaagentclassregister

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!