I got an IllegalAccessError when trying to use PowerMock with JUnit

Introduction

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.

Symptoms

//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>

analysis

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`.

Solution

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.

at the end

It was helpful to have an easy-to-understand Q & A.

Recommended Posts

I got an IllegalAccessError when trying to use PowerMock with JUnit
I got an InvalidUseOfMatchersException when using any with JUnit Mock
When I bcrypt with node + docker, I got an error
I got an error when trying to install sbt to build a Scala development environment
A note that I had trouble when trying to use nginx with Remote-Containers of vscode
I was addicted to WSl when trying to build an android application development environment with Vue.js
I want to use DBViewer with Eclipse 2018-12! !!
Error when trying to use heroku command
When I tried to build an environment of PHP7.4 + Apache + MySQL with Docker, I got stuck [Windows & Mac]
I want to use java8 forEach with index
I got stuck when port forwarding with VBox
I got an error when I ran rake routes.
I get an error when I try to use "^" or "$" in ruby ​​regular expression
[Error resolution] Occurs when trying to build an environment for spring with docker
Summary when trying to use Solr in Java and getting an error (Solr 6.x)
When I tried to use a Wacom tablet with ubuntu 20.04, I didn't recognize it.
When registering a new user, I got an error called ActiveRecord :: NotNullViolation and how to deal with it.
How to use JUnit 5
What I was addicted to when trying to properly openAPI/Swagger documentation with Rails + Grape + Grape Swagger
A warning is displayed when trying to use a huge integer with the special variables $ 1, $ 2, $ 3 ...
Webdrivers :: BrowserNotFound: Failed to find Chrome binary. When I was trying to test E2E with Docker + Rails for the first time, I got stuck in an error.
When trying to insert a specific gem (levenshtein) with Docker, an error occurred without ruby-dev
I got an error when using nextInt, nextLine and substring.
I tried to make an introduction to PHP + MySQL with Docker
I want to push an app made with Rails 6 to GitHub
What to do when you launch an application with rails
How to specify db when creating an app with rails
What happens to instance variables when copying an instance with ruby
I want to manually send an authorization email with Devise
[Java] I want to test standard input & standard output with JUnit
How to use JUnit (beginner)
[Creating] How to use JUnit
When I try to use the AWS SDK with Ruby + Lambda, `sam local` is messed up.
I was angry with proc_open (): fork failed when trying to composer update inside a Docker container
I want to convert an array to Active Record Relation with Rails
When I push to Heroku, I get angry with Precompiling assets failed.
I tried to build an http2 development environment with Eclipse + Tomcat
What I was addicted to when implementing google authentication with rails
I got an error and couldn't push! !! [error: failed to push some refs to]
I tried to make an Android application with MVC now (Java)
I got stuck trying to write a where in clause in ActiveRecord
[Rails] I want to add data to Params when transitioning with link_to
Resolved the error that occurred when trying to use Spark in an environment where Java 8 and Java 11 coexist.
I want to use FormObject well
I tried to interact with Java
How to use mssql-tools with alpine
I tried to summarize the stumbling points when developing an Android application
When I ran a jar created using WildFly Swarm, I got an InvocationTargetException.
Problems I was addicted to when building the digdag environment with docker
I tried to make an automatic backup with pleasanter + PostgreSQL + SSL + docker
Rubocop gets angry when I try to fill zeros (fill 0s) with Ruby
01. I tried to build an environment with SpringBoot + IntelliJ + MySQL (MyBatis) (Windows10)
I want to display an error message when registering in the database
I want to ForEach an array with a Lambda expression in Java
When I try to sign up with devise, it automatically redirects to root_path
Since the du command used when the capacity is full is difficult to use, I tried wrapping it with ruby
I built a CentOS 8 environment with Vagrant and tried to sync_folder, but I got an error, so I solved it.
I tried to use docker with jetson jetpack4.3 but got an error (docker: Error response from daemon: OCI runtime create failed: container_linux.go: 345: starting container process caused ...)