How to test void method with private method calls using Mockito

Ankit

I have the following piece of code

public class A extends B {
private boolean workDone = false;
@Override
public void publicMethod(boolean flag) {
  if (!workDone) {
    privateMethod();
    workDone = true;
  }
  super.publicMethod(flag);
 }
 private void privateMethod() {
  // some logic here
 }
}

I'm new to mocking. I have following doubts. I'm trying to test the public method.

  1. is it possible for me to assert the value of private variable workDone?
  2. is it possible to verify the method call in the super class?
  3. How can I mock the private method call in the method?
kism3t

If you really want to verify it, you need to change your A class and extract the super call into a private method:

public class A extends B {

    private boolean workDone = false;

    @Override
    public void publicMethod(final boolean flag) {
        if (!workDone) {
            privateMethod();
            workDone = true;
        }
        callParentPublicMethod(flag);
    }

    private void callParentPublicMethod(final boolean flag) {
        super.publicMethod(flag);
    }

    private void privateMethod() {
        System.out.println("A: privateMethodCalled");
    }
}

after this is done you can use PowerMock to verify private method invocations:

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

@RunWith(PowerMockRunner.class)
@PrepareForTest({ A.class })
public class ATest {

    @Test
    public void publicMethod_test_false() throws Exception {

        A spy = PowerMockito.spy(new A());
        spy.publicMethod(false);
        PowerMockito.verifyPrivate(spy).invoke("privateMethod");
        PowerMockito.verifyPrivate(spy).invoke("callParentPublicMethod", false);
    }

    @Test
    public void publicMethod_test_true() throws Exception {

        A spy = PowerMockito.spy(new A());
        spy.publicMethod(true);
        PowerMockito.verifyPrivate(spy).invoke("privateMethod");
        PowerMockito.verifyPrivate(spy).invoke("callParentPublicMethod", true);
    }
}

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

In Java, How to test void method in Junit Mockito with assertion?

mock nested method calls using mockito

How to stub return value for the private method of same class using mockito

Test void method behavior junit and mockito

Java verify void method calls n times with Mockito

Mockito test a void method throws an exception

Mockito different behavior on subsequent calls to a void method?

Testing Private method using mockito

How to mock/test method that returns void, possibly in Mockito

Using Mockito, how do I intercept a callback object on a void method?

Mockito java - mock third party utility methods and private method calls

How to test method in Junit4 which calls a private method

How to Test void Method with spring repository using Junit and Mockito

How test a void method into Service class, with Mockito?

Mockito / Powermockito mock private void method

Mockito calls stubbed method even if using doReturn

How to mock chain of method calls using Mockito

Mockito UnfinishedStubbingException thrown on a JUnit test case which calls a void ServiceImplementationLayer method

How to test a void method using JUnit and/or Mockito

How to test a void method within a Presenter using Mockito?

NullPointerException when trying to test a void method in Presenter using Mockito

how can i test void method using google test framework

Testing a method which calls private void method inside using mockito

Mockito - Testing method that calls private methods

How to unit test a public method that calls private method and that in turn calls a web service method

Using mockito to stub a void method

Unit test a method with a call to a private callback using Mockito|Android

How to unit test a method which calls a void method?

test void method using mockito and Junit with generic parameter