question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mockito java.lang.NoSuchMethodError: org.mockito.internal.matchers.InstanceOf.<init>

See original GitHub issue

I 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:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
lxyangfancommented, Feb 14, 2019

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.

0reactions
dijofranciscommented, Aug 6, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

16 - Stack Overflow
For some reason, your test suite, tries to load the MockitoJunitRunner from the org.mockito.junit contained in the Mockito versions >= 2.
Read more >
java.lang.NoSuchMethodError org.mockito.internal.creation ...
Hello,. I using powermock with mockito et junit4. When i made this test. @RunWith(PowerMockRunner.class) @PrepareForTest(classStatique.class)
Read more >
org.mockito.internal.matchers.InstanceOf.<init> java code ...
How to use. org.mockito.internal.matchers.InstanceOf. constructor. Best Java code snippets using org.mockito ...
Read more >
Matchers.instanceOf method does not compile - Treehouse
instanceOf method used in detail_ShouldErrorOnNotFound() is not recognized by ... MockitoJUnitRunner; import org.mockito.internal.matchers.
Read more >
java.lang.nosuchmethoderror org.mockito.internal ... - 掘金
java.lang.nosuchmethoderror org.mockito.internal.matchers.instanceof. init技术、学习、经验文章掘金开发者社区搜索结果。掘金是一个帮助开发者成长的社区 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found