3

I'm trying to set up a unit test with Mockito and PowerMockito, but it throws:

Exception in thread "main" java.lang.NoClassDefFoundError: org/mockito/exceptions/Reporter

whenever I try to run a test. These are my dependencies:

testCompile 'org.mockito:mockito-core:2.8.9'
testCompile 'org.powermock:powermock-api-mockito2:1.6.5'
testCompile 'org.powermock:powermock-module-junit4:1.7.4'

Does anyone know how to fix it?

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
aloj
  • 3,194
  • 7
  • 27
  • 38

2 Answers2

8

It seems you are trying to use incompatible versions.

According to this table, you need to use version 1.7.x of Powermock in order to be compatible with your Mockito version 2.8.9.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
1

Add these dependencies in your external-dependencies.xml-

<dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-support</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-reflect</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.5.6</version>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4-common</artifactId>
        <version>1.5.6</version>
    </dependency>

Explanation-

  1. powermock 1.5.6 has issue with junit 4.12 - that requires powermock 1.6.1 :: org.powermock.reflect.exceptions.FieldNotFoundException: Field 'fTestClass' was not found in class

  2. org.junit.internal.runners.MethodValidator powermock 1.6.1 requires more current mockito :: No methods matching the name(s) add were found in the class hierarchy of interface org.mockito.internal.util.MockitoSpy.

  3. Switching to powermock 1.6.2 with upgraded mockito to 1.10.8:: results java.lang.NoClassDefFoundError: org/mockito/internal/creation/util/MockitoMethodProxy switch to mockito-core 1.10.19 and org.powermock 1.6.4

Farrukh Chishti
  • 7,652
  • 10
  • 36
  • 60