How do you expect and verify a private void method call using PowerMockito and TestNG?

kurofune

I need to write unit tests against a pre-existing code base using TestNG, Mockito and now PowerMockito, to test private and static methods more easily. I am currently trying to write a test against a private void method in a class that we are testing, but am unable to figure it out. In the normal PowerMock API there are methods called replayAll(), verifyAll(), and expectLastCalled(), which are suitable for most purposes. I just can't find good docs that explain how to do it the PowerMockito way. Any explanations or insights on this would be much appreciated.

Method to test:

private void pVMethod(Type param) throws Exception {

    param.setA(StaticClass.methodA().toString());
    param.setB(StaticClass.methodB().toString());

    // getMemo(String label) is in a public class in same package
    param.setC(getMemo("memo.provider"));
    param.setD(getMemo("memo.item"));
        try {
             param.setTimestamp(DataTypeFactory.newInstance().newXMLGregorianCalendar(newjava.util.GregorianCalendar()));
        } catch (SomeException e) {
           ...
          throw new Exception();
    }
}

test attempt:

@Test(expectedExceptions = Exception.class)
public void pVMethod() throws Exception {
    TestClass testMock = mock(TestClass.class);
    Exception exception = mock(Exception.class);

    // StaticClass staticClassMock = mock(StaticClass.class); ??
    mockStatic(StaticClass.class);

    // when(..) and thenReturn(..) are static imports from PowerMockito library
    when(StaticClass.methodA()).thenReturn("stubStringA");
    when(StaticClass.methodB()).thenReturn("stubStringB");

    doThrow(exception).when(param).setTimestamp(Mockito.any(XMLGregorianCalendar.class));

    // Docs say method name can be inferred via reflection
    Whitebox.invokeMethod(tested, event);

    // this is where things are hairy. testedSpy is defined at the top level
    verifyPrivate(testedSpy).pVMethod(testMock);
}
kurofune

Ok, here is the answer:

In PowerMockito, if you want to verify the behavior of a private void method you use the verifyPrivate() method, but you have to do it like this:

verifyPrivate(tested).invoke("privateMethodName", argument);

Notice the use of the invoke method, missing from the last line of the OP.

NOTE: You do not have to use a doNothing().when(mock.privateMethod()) statement, because void methods in mock objects don't do anything by default.

example taken from here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Mock a private void method using PowerMockito

Verify Static Method Call using PowerMockito 1.6

How do I verify a call to a static method in a final class using jMockit and TestNG?

Verify private static method on final class gets called using PowerMockito

PowerMockito verify private method called x times

Mockito / Powermockito mock private void method

powermockito spy private void method invocation

Mocking private method call using PowerMockito returns null instead of returning List<String>: (Want not to execute private method)

How to Mock private method which is called from the constructor using PowerMockito

Mocking a private method in junit using Powermockito

Mockito - how to verify a method call made by an indirect private field object

Mockito verify method call inside void method

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

How to verify an internal method call using Powermock?

How to hide all items in a private void method you've created?

How do you assert with delete method void

How to test void method with private method calls using Mockito

How to call a void method in java using reflection

How to verify inputs to a private method?

Using PowerMockito how do I verify a constructor was called, with a specific set of arguments

Verify a private method gets call Unit Test

How to verify to call or not to call a method

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

How to do the Assert phase of a private method whose return type is Void?

Verify a method call using Moq

How do you call this method ( Angular 5 )?

Mockito: How do I verify method call with arguments on JPA Specification?

How can I mock private static method with PowerMockito?

How to verify a method call with Enum Parameter using Mockito?