Use PowerMock learning (XI) of the Mock private methods of

Mock  private methods

It is to mock private methods, learned this not difficult to find, in fact, most of us are going to complete the unit testing by reflection, but in practice, a class private methods, individuals do not recommend the use of reflection to test, because there are when a unit test methods will be covered, so let's just give you an example, be used to simulate a mock test private methods.

Simulation scenarios

Suppose we follow the names to find whether there is a student ISEXIST () , returns true if it exists, otherwise false, while calling this private lookup method returns the number of students in the class, returns 1 if there is, there is no return 0

Business logic code is simple, specific code as follows:

package com.rongrong.powermock.mockprivate;

/**
 * @author rongrong
 * @version 1.0
 * @description:
 * @date 2019/12/5 21:48
 */
public class StudentPrivateService {

    public boolean isExist(String userName) {
        int count = checkExist(userName);
        if (count > 0) {
            return true;
        } else {
            throw new NullPointerException();
        }

    }

    private int checkExist(String userName) {
        throw new UnsupportedOperationException();
    }


}

Then we are powermock tested using AnSwer and spy methods learned in an article to be tested.

Specific code as follows:

package com.rongrong.powermock.mockprivate;

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

import static junit.framework.TestCase.assertTrue;

/**
 * @author rongrong
 * @version 1.0
 * @description:
 * @date 2019/12/5 21:54
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest(StudentPrivateService.class)
public class TestStudentPrivateService {

    @Test
    public void testStudentPrivateService(){
        StudentPrivateService studentPrivateService = PowerMockito.spy(new StudentPrivateService());
        try {
            PowerMockito.doAnswer(new Answer<Integer>() {
                @Override
                public Integer answer(InvocationOnMock invocation) throws Throwable {
                    String arg= (String) invocation.getArguments()[0];
                    if("rongrong".equals(arg)){
                        return 1;
                    }else {
                        return 0;
                    }
                }
            }).when(studentPrivateService,"checkExist","rongrong");
            boolean exist = studentPrivateService.isExist("rongrong");
            assertTrue(exist);
            PowerMockito.verifyPrivate(studentPrivateService).invoke("checkExist","rongrong");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This whole powermock learning is complete, have to say, powerful multi powermock than expected.

Guess you like

Origin www.cnblogs.com/longronglang/p/11992531.html