In the JUnit test, I tried to install PowerMock because I wanted to use it, but an error occurred, so I will summarize the solution.
//Foo test
public class FooTest {
	@Test
    public void testFoo() {
        //Create a Mock
        Bar bar = mock(Bar.class);
        
        //Various tests
    }
}
I wrote a test code like this before installing PowerMock, and it worked fine.
When I added PowerMock to pom.xml, I got the following error when running the test.
java.lang.IllegalAccessError: class org.mockito.internal.creation.jmock.ClassImposterizer$1 cannot access its superclass org.mockito.internal.creation.cglib.MockitoNamingPolicy
In pom.xml, the part that seems to be related is like this.
<dependencies>
  <!-- Spring Boot Starter Test -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
  <!-- Powermock -->
  <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.5.4</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.5.4</version>
    <scope>test</scope>
  </dependency>
  <!--Other external libraries-->
</dependencies>
When I tried google, this Q & A was helpful.
stackoverflow - Junit Mockito error on initialization
According to this, spring-boot-starter-test 1.4.2.RELEASE uses Mockito 1.10.19 by default, and ʻorg.mockito.internal.creation.jmock.ClassImposterizer on 1.10.19. It seems that `is not included.
In my environment, I am using spring-boot-starter-test 1.5.14.RELEASE, and when I check the Maven dependency, I find that it also uses Mockito 1.10.19. I did.
It seems that 1.9.5 contains ʻorg.mockito.internal.creation.jmock.ClassImposterizer`.
Modify pom.xml as follows.
<dependencies>
  <!-- Spring Boot Starter Test -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <!--Exclude Mockito-->
    <exclusions>
      <exclusion>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
  <!--Added Mockito-->
  <dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.9.5</version>
    <scope>test</scope>
  </dependency>
  <!-- Powermock -->
  <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.5.4</version>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.5.4</version>
    <scope>test</scope>
  </dependency>
  <!--Other external libraries-->
</dependencies>
Now the error is gone.
It was helpful to have an easy-to-understand Q & A.
Recommended Posts