Java.lang.nosuchmethoderror: ‘org.mockito.stubbing.answer org.mockito.answers.get()’

java.lang.nosuchmethoderror: ‘org.mockito.stubbing.answer org.mockito.answers.get()’

This error indicates that a method is being called on an object using Mockito library and the method being called doesn’t exist in the current version of the library or the method has been removed or its signature has changed.

To fix this error, you need to identify the method causing the issue and make the necessary changes in your code. Here are a few possible solutions:

  • Check if you are using the correct version of Mockito library. Make sure to use a version that includes the method you are calling.
  • Verify the method name and its parameters. The method name or signature might have changed in a newer version of Mockito.
  • If you are using any custom or third-party libraries that rely on Mockito, ensure that they are compatible with the Mockito version you are using.

Here’s an example where this error might occur:

    
      import org.mockito.Mockito;

      public class Example {
          public static void main(String[] args) {
              // Mocking a class
              MyClass myMock = Mockito.mock(MyClass.class);

              // Incorrect method call causing the error
              Answer myAnswer = myMock.get(); // Assuming the 'get' method doesn't exist or has changed in the current Mockito version

              // Rest of the code...
          }
      }
    
  

In the above example, calling the non-existent or changed method ‘get’ on ‘myMock’ object would result in the ‘java.lang.nosuchmethoderror’.

Make sure to update your code to use the correct method or switch to a compatible version of the Mockito library to resolve this error.

Similar post

Leave a comment