powermock-api-mockito2 does not work with Mockito 4 due to using deprecated API(s)

See original GitHub issue

Using powermock-api-mockito2 2.0.9.

With mockito 3.12.4 this code works fine:

import org.mockito.Mock;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.Test;

public class SimpleTest extends PowerMockTestCase {

    @Mock
    private SimpleInterface simpleInterface;

    @Test
    public void testSimpleTest() {
        
    }

    interface SimpleInterface {}
}

but with the newly released mockito 4.0.0, the following Exception is thrown:

java.lang.NoSuchMethodError: org.mockito.Answers.get()Lorg/mockito/stubbing/Answer;

	at org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:85)
	at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:55)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1864)
	at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:715)
	at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:415)
	at org.powermock.modules.testng.PowerMockTestCase.injectMocks(PowerMockTestCase.java:135)
	at org.powermock.modules.testng.PowerMockTestCase.beforePowerMockTestMethod(PowerMockTestCase.java:85)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
	at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62)
	at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:385)
	at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:321)
	at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:700)
	at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:527)
	at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
	at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
	at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
	at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
	at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
	at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)

Due to removal of this deprecated method in org.mockito.Answers:

    /** @deprecated */
    @Deprecated
    public Answer<Object> get() {
        return this;
    }

org.powermock.api.extension.listener.AnnotationEnabler#standardInject

                if (answers != null) {
                    mockSettings.defaultAnswer(answers.get());
                }

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:14
  • Comments:5

github_iconTop GitHub Comments

11reactions
m2gercommented, Dec 8, 2021

I know this is not the answer You want to hear, but we upgraded Mockito to 4.0.0 and stopped using PowerMock altogether because Mockito allows to mock static methods since 3.4.0, which was the only reason we used PowerMock.

3reactions
apalcloudcommented, Jun 2, 2022

how does Mockito 4.0.0 support to mock private method?

Read more comments on GitHub >

github_iconTop Results From Across the Web

pom.xml - Stack Overflow
mockito.Answers was deleted, it was already deprecated in 3.12.4, and the powermock-api-mockito was not adapted to work with the newer version.
Read more >
Deprecated List (powermock-api-mockito 1.7.1 API) - javadoc.io
Test Runners uses an annotation enabling listener per default since version 1.3. You should just remove this listener.
Read more >
Mockito 2.x over PowerMock Migration: Top 10 Tips and Tricks
Using powermock-api-mockito extension does not work with Mockito 2.x, you will have the following exception when running your unit tests if ...
Read more >
Guide - 7.10 Migration Mockito 1 to Mockito 2
The standard Mockito2 runner (strict runner) detects unused stubs. So test code becomes minimal and its readability is improved · The methods anyX()...
Read more >
Kubernetes 1.25 deprecated APIs - Google Cloud
The Beta API version ( discovery.k8s.io/v1beta1 ) of EndpointSlice is no longer served as of version 1.25. This API was deprecated in version...
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