Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerMockRule ClassNotFoundException is thrown

I have problem with PowerMock and Mockito compatibility.

I have test class:

@ActiveProfiles("test")
@PrepareForTest(LanguageUtils.class)
public class MyControllerTest {

    @Mock
    private MyMockClass MyMockClass;    
    @Autowired
    private MyController myController;

    @Rule
    public PowerMockRule rule = new PowerMockRule();
}

My POM looks like:

    <junit-version>4.12</junit-version>
    <hamcrest-version>1.3</hamcrest-version>
    <mockito-version>1.10.19</mockito-version>      
    <powermock-version>1.6.4</powermock-version>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>${hamcrest-version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>${mockito-version}</version>
        <exclusions>
            <exclusion>
                <artifactId>hamcrest-core</artifactId>
                <groupId>org.hamcrest</groupId>
            </exclusion>
        </exclusions>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>${powermock-version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>${powermock-version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4-rule</artifactId>
        <version>${powermock-version}</version>
        <scope>test</scope>
    </dependency>

When I try to run test I get ClassNotFoundException. Error cause PowerMockRule because when I delete this line error disappears.

Full stacktrace looks like:

java.lang.RuntimeException: java.lang.ClassNotFoundException: org.powermock.classloading.DeepCloner
    at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:68)
at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:34)
at org.powermock.classloading.ClassloaderExecutor.createDeepCloner(ClassloaderExecutor.java:106)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:88)
at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:57)
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:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.powermock.classloading.DeepCloner
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.powermock.api.support.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:66)
... 20 more
like image 498
4the3eam Avatar asked Jan 05 '23 15:01

4the3eam


1 Answers

This is a known issue with PowerMock (see, e.g., bug 597). In short, if you replace the rule module with the rule-agent module, it should do the trick. I.e., replace this requirement:

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4-rule</artifactId>
    <version>${powermock-version}</version>
    <scope>test</scope>
</dependency>

With:

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4-rule-agent</artifactId>
    <version>${powermock-version}</version>
    <scope>test</scope>
</dependency>
like image 161
Mureinik Avatar answered Jan 07 '23 06:01

Mureinik