Powermock throws InvalidUseOfMatchersException when mocking private method

ntson9p

I'm new to Powermock. Yesterday I watched a video about using Powermock, I follow their code and have a demo test program below. When I run the test

package myPackage;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class Test1{
    @Test
    public void testPrivateMethod() throws Exception {
        DemoService mockDemoService = PowerMockito.spy(new DemoService());
        PowerMockito.when(
                mockDemoService,
                "privateMethod",
                ArgumentMatchers.any(String.class)
        ).then(invocation -> invocation.getArgument(0) + "_private_mock");
        Assert.assertEquals(mockDemoService.dependentMethod("LoveTest"), "3000LoveTest_private_mock");
    }
}

class DemoService {
    private String privateMethod(String input) {
        return input + "_private";
    }
    public String dependentMethod(String input) {
        return 3000 + privateMethod(input);
    }
}

It throw exception in the log below

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced or misused argument matcher detected here:

-> at myPackage.Test1.testPrivateMethod(Test1.java:19)

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

This message may appear after an NullPointerException if the last matcher is returning an object 
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
    when(mock.get(any())); // bad use, will raise NPE
    when(mock.get(anyInt())); // correct usage use

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.

I have some basic knowledge about Mockito, the code is somewhat similar to Mockito unit test (Mockito.spy and Mockito.when) but not work as expected. I don't know what's wrong with the code, why the exception InvalidUseOfMatchersException is thrown. Please help me, thank you.

code đờ

Your code is missing @PrepareForTest. Powermock has to manipulate the byte-code of target mocking class, so you have to provide a path to it.

@RunWith(PowerMockRunner.class)
@PrepareForTest(DemoService.class)
public class Test1{
    ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mocking private method with PowerMock

Why when mocking a private method using Powermock is calling the real method?

Java Mockito InvalidUseOfMatchersException when mocking method

PowerMockito: got InvalidUseOfMatchersException when use matchers mocking static method

PowerMock issue when mocking a static method with Java7 construct

Mocking static method inside static method on powermock

Junit Mocking Private Method

Receiving InvalidUseOfMatchersException when stubbing method

Private method invocation using Powermock

java.lang.ExceptionInInitializerError when mocking static method using EasyMock+PowerMock

Static Field as Null when mocking Enums with PowerMock

Is it possible to use partial mocking for private static methods in PowerMock?

How to mock private method using powermock?

How to mock private method for testing using PowerMock?

Why mocking a private method enters method?

Mocking public method inside a private method

Unit Tests, mocking static method using Context as variable in Kotlin with PowerMock

PowerMock: mocking multiple calls of same method with different parameters behave abnormally

Powermock throws ClassNotPreparedException when using JUnit 5

Mocking a private method in junit using Powermockito

Mocking an object returned from private method in java

Power Mockito Throws "Unfinished stubbing" error when method is private, but not error when method is protected

Verify a void method was called inside a private method with EasyMock/PowerMock

Mocked private method with PowerMock, but underlying method still gets called

NPE when mocking Annotation method

Mockito InvalidUseOfMatchersException, when trying to unit test method with custom Callback as a parameter

PowerMock: WhiteBox.getMethod() : Getting a private method with no parameters

PowerMock complains of incorrect arguments even though the private method is mocked

Mocking private method with PowerMockito only works starting with second method call