How to fix MockitoExtension.class not resolved error
Solution 1
I had the same problem in a simple test project and it turned out that I have only added the core artifact:
org.mockito:mockito-core
and I also needed to add the one that contains the extension for jUnit5:
org.mockito:mockito-junit-jupiter
Solution 2
In my pom.xml (I left to the version
if is already used for other dependency).
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
</dependency>
But, still the class was not found.!
In my class I was testing with:
import org.junit.jupiter.api.extension.ExtendWith;
@ExtendWith(org.mockito.junit.jupiter.MockitoExtension.class)
public class MyClassTest {
//...
}
And it stopped asking for the import....
Then, I put manually the import, then Secondary Click
-> Source
-> Organize Imports
.
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class MyClassTest {
//...
}
Solution 3
Add this to your test folder as MokitoExtension.java
package com.pluralsight.tddjunit5;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.lang.reflect.Parameter;
import static org.mockito.Mockito.mock;
public class MockitoExtension
implements TestInstancePostProcessor, ParameterResolver {
@Override
public void postProcessTestInstance(Object testInstance,
ExtensionContext context) {
MockitoAnnotations.initMocks(testInstance);
}
@Override
public boolean supportsParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) {
return parameterContext.getParameter().isAnnotationPresent(Mock.class);
}
@Override
public Object resolveParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) {
return getMock(parameterContext.getParameter(), extensionContext);
}
private Object getMock(Parameter parameter,
ExtensionContext extensionContext) {
Class<?> mockType = parameter.getType();
Store mocks = extensionContext
.getStore(Namespace.create(MockitoExtension.class, mockType));
String mockName = getMockName(parameter);
if (mockName != null) {
return mocks.getOrComputeIfAbsent(mockName,
key -> mock(mockType, mockName));
} else {
return mocks.getOrComputeIfAbsent(mockType.getCanonicalName(),
key -> mock(mockType));
}
}
private String getMockName(Parameter parameter) {
String explicitMockName = parameter.getAnnotation(Mock.class).name()
.trim();
if (!explicitMockName.isEmpty()) {
return explicitMockName;
} else if (parameter.isNamePresent()) {
return parameter.getName();
}
return null;
}
}
Solution 4
I had the same problem with Gradle. The MockitoExtension class is actually in the mockito-junit-jupiter.jar file, therefore mockito-core is not required. See the screen clipping below:
Here is a screen clipping of my Gradle file:
Here is a link on an article that might help: https://www.vogella.com/tutorials/Mockito/article.html
Solution 5
If you are working with Junit5, you need to include this dependency org.mockito:mockito-junit-jupiter
.
However, with Junit4, you don't need to use this extension and can achieve the same mocking behaviour by using Junit4's @RunWith and MockitoJUnitRunner.class
. But in Junit5, there are no rules and runners. hence you cannot use @RunWith(MockitoJUnitRunner.class)
or Rules in Junit5.
Comments
-
Šimon almost 2 years
I have recently migrated from JUnit4 to JUnit5. I have updated and edited my POM files (multiple POMs because my project is a multi-maven project).
It seems that my IDE (IntellIj Idea) is resolving JUnit 5 annotations. They are all available and functional.
But, when I am trying to annotate
OwnerSDJpaServiceTest
with@ExtendWith(MockitoExtension.class)
, IntellIj keeps telling me that it "cannot resolve symbol 'MockitoExtension'.To me, it looks like I am missing some dependencies but I have looked at youtube videos and some documentations and it looks all good to me. So I don't know where might the real problem be.
Maybe I am missing a dependency or I have a misconfigured POM files.
If you need to see the whole app please use this link to my GitHub repo.
Here is my root pom file:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>guru.springframework</groupId> <artifactId>sfg-pet-clinic</artifactId> <version>0.0.5-SNAPSHOT</version> <modules> <module>pet-clinic-data</module> <module>pet-clinic-web</module> </modules> <packaging>pom</packaging> <name>sfg-pet-clinic</name> <description>SFG Pet Clinic Project</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF 8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-release-plugin</artifactId> <configuration> <goals>install</goals> <autoVersionSubmodules>true</autoVersionSubmodules> </configuration> </plugin> </plugins> </build> <scm> <developerConnection>scm:git:https://github.com/sajmon2325/Spring-Pet-Clinic.git</developerConnection> <tag>HEAD</tag> </scm> </project>
Here is my pet-clinic-data pom file
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>sfg-pet-clinic</artifactId> <groupId>guru.springframework</groupId> <version>0.0.5-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>pet-clinic-data</artifactId> <properties> <spring.boot.repackage.skip>true</spring.boot.repackage.skip> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.0.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.23.1-GA</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <skip>true</skip> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <dependencies> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>2.27.0</version> <scope>runtime</scope> </dependency> </dependencies> </plugin> </plugins> </build> </project>
Here is my pet-clinic data pom file
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>sfg-pet-clinic</artifactId> <groupId>guru.springframework</groupId> <version>0.0.5-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>pet-clinic-data</artifactId> <properties> <spring.boot.repackage.skip>true</spring.boot.repackage.skip> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.0.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.23.1-GA</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <skip>true</skip> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <dependencies> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>2.27.0</version> <scope>runtime</scope> </dependency> </dependencies> </plugin> </plugins> </build> </project>
And finally here is my pet-clinic-web pom file (which is probably missing a dependency):
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>sfg-pet-clinic</artifactId> <groupId>guru.springframework</groupId> <version>0.0.5-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>pet-clinic-web</artifactId> <properties> <!-- Web dependencies --> <webjars-bootstrap.version>3.3.6</webjars-bootstrap.version> <webjars-jquery-ui.version>1.11.4</webjars-jquery-ui.version> <webjars-jquery.version>2.2.4</webjars-jquery.version> <wro4j.version>1.8.0</wro4j.version> </properties> <dependencies> <dependency> <artifactId>pet-clinic-data</artifactId> <groupId>guru.springframework</groupId> <version>0.0.5-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <!-- webjars --> <dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator-core</artifactId> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>${webjars-jquery.version}</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery-ui</artifactId> <version>${webjars-jquery-ui.version}</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>${webjars-bootstrap.version}</version> </dependency> <!-- end of webjars --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>ro.isdc.wro4j</groupId> <artifactId>wro4j-maven-plugin</artifactId> <version>${wro4j.version}</version> <executions> <execution> <phase>generate-resources</phase> <goals> <goal>run</goal> </goals> </execution> </executions> <configuration> <wroManagerFactory>ro.isdc.wro.maven.plugin.manager.factory.ConfigurableWroManagerFactory</wroManagerFactory> <cssDestinationFolder>${project.build.directory}/classes/static/resources/css</cssDestinationFolder> <wroFile>${basedir}/src/main/wro/wro.xml</wroFile> <extraConfigFile>${basedir}/src/main/wro/wro.properties</extraConfigFile> <contextFolder>${basedir}/src/main/less</contextFolder> </configuration> <dependencies> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>${webjars-bootstrap.version}</version> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>2.23.4</version> </dependency> </dependencies> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> <dependencies> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>2.27.0</version> <scope>runtime</scope> </dependency> </dependencies> </plugin> </plugins> </build> </project>
Here is my test class (which has the problem of not resolved symbol in MockitoExtension.class):
package guru.springframework.sfgpetclinic.services.springdatajpa; import guru.springframework.sfgpetclinic.repositories.OwnerRepository; import guru.springframework.sfgpetclinic.repositories.PetRepository; import guru.springframework.sfgpetclinic.repositories.PetTypeRepository; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import static org.junit.jupiter.api.Assertions.*; @ExtendWith(MockitoExtension.class) class OwnerSDJpaServiceTest { OwnerRepository ownerRepository; PetRepository petRepository; PetTypeRepository petTypeRepository; OwnerSDJpaService service; @BeforeEach void setUp() { } @Test void findByLastName() { } @Test void findAll() { } @Test void findById() { } @Test void save() { } @Test void delete() { } @Test void deleteById() { } }
I expect that the annotation is reckognized by IntellIj so I can test this class using JUnit5.