Mockito java.lang.NoSuchMethodError: org.mockito.internal.matchers.InstanceOf.<init>
See original GitHub issueI am trying to create a unit test for an stateless bean class that is calling CompletionService and returning a future object. I am working in Eclipse Oxygen with Junit 5 and Mockito 2.21 core. I cannot get the dependencies to be successfully completed it starts with error:
java.lang.NoSuchMethodError: org.mockito.internal.matchers.InstanceOf.<init>(Ljava/lang/Class;Ljava/lang/String;)V at org.mockito.internal.matchers.InstanceOf$VarArgAware.<init>(InstanceOf.java:45) at org.mockito.ArgumentMatchers.any(ArgumentMatchers.java:207) at com.sbsa.psync.advise.generate.GetCustomerServiceV4Test4.testGetCustomer(GetCustomerServiceV4Test4.java:137)
The Unit test I am trying to execute
@Test
public void testGetCustomer()
{
LogService logService = mock(LogService.class);
doNothing().when(logService).addActivityLogEntry(any(LogCommonFields.class), any(String.class), any(String.class));
when(logService.getCodeDetails(any(String.class))).thenReturn("SUCCESS");
doNothing().when(logService).logToPerformanceLogFormatted(any(String.class), any(String.class), any(Long.class));
}
I am Using Eclipse Oxygen with Junit 5 and Mockito 2.21, Java 1.8, hamcrest 2.0 on Windows 10 The only errors I could find that seem to be remotely related was related to the hamcrest dependency that might be shared between JUnit and Mockito. I then added the below tags in the pom file with no effect.
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.21.0</version>
<exclusions>
<exclusion>
<artifactId>hamcrest-core</artifactId>
<groupId>org.hamcrest</groupId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
Below is a simple class and test that gives the same error.
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import com.sbsa.psync.common.LogCommonFields;
@Stateless
@LocalBean
@TransactionManagement(TransactionManagementType.BEAN)
public class LogService2
{
public void addActivityLogEntry(LogCommonFields logCommonFields, String code, String bpId)
{
System.out.println("logging the error");
}
}
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import org.junit.jupiter.api.Test;
import com.sbsa.psync.common.LogCommonFields;
import com.sbsa.psync.log.LogService2;
class GetCustomerServiceV4Test5
{
@Test
public void testGetCustomer()
{
LogService2 logService = mock(LogService2.class);
doNothing().when(logService).addActivityLogEntry(any(LogCommonFields.class), any(String.class), any(String.class));
}
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top GitHub Comments
refer to https://stackoverflow.com/questions/7869711/getting-nosuchmethoderror-org-hamcrest-matcher-describemismatch-when-running
I find I have both “mockito-all” and “mockito-core” dependency in my project. So I just exclude “mockito-all” and the problem is solved.
@lxyangfan same here