Mock Answer PowerMock use of learning (nine) of

About Mock Answer

Previous article, there are introduced using about Arguments Matche, in fact, the role of Answer with its relatively similar, but it is more powerful than Arguments Matcher.

Arguments Matche

I.e. different parameters passed, and returns the results, focusing on the determination of parameters, the parameters in the override method to judge

Answer

See EENOW name, i.e. return different results, but to judge based on the incoming parameters, return to the overridden method determination result is returned

Simulation scenarios

Find the name of the student mailboxes, controller layer transfer service

service layer

Specific code examples are as follows:
package com.rongrong.powermock.answers;

/**
 * @author rongrong
 * @version 1.0
 * @description:
 * @date 2019/12/4 20:24
 */
public class StudentAnswerService {

    public String getEmail(String userName){
        throw new UnsupportedOperationException();
    }
}

controller layer

Specific code examples are as follows:

package com.rongrong.powermock.answers;

/**
 * @author rongrong
 * @version 1.0
 * @description:
 * @date 2019/12/4 20:24
 */
public class StudentController {

    public String getEmail(String userName) {
        StudentAnswerService studentAnswerService = new StudentAnswerService();
        return studentAnswerService.getEmail(userName);
    }
}

 

The above code business code is relatively simple, the following test again

Specific examples of code is as follows:

package com.rongrong.powermock.answers;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
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.assertEquals;
import static junit.framework.TestCase.fail;

/**
 * @author rongrong
 * @version 1.0
 * @description:
 * @date 2019/12/4 20:34
 */
@RunWith(PowerMockRunner.class)
//准备调用层的类
@PrepareForTest(StudentController.class)
public class TestStudentAnswerService {

    @Test
    public void testStudentAnswerService() {
        StudentAnswerService studentAnswerService = PowerMockito.mock(StudentAnswerService.class);
        PowerMockito.when(studentAnswerService.getEmail(Mockito.anyString())).then(new Answer<String>() {
            @Override
            public String answer(InvocationOnMock invocation) throws Throwable {
                String arg = (String) invocation.getArguments()[0];
                if ("rr".equals(arg)) {
                    return "[email protected]";
                } else if ("jqj".equals(arg)) {
                    return "[email protected]";
                }
                throw new NullPointerException();
            }
        });
        try {
            PowerMockito.whenNew(StudentAnswerService.class).withAnyArguments().thenReturn(studentAnswerService);
            StudentController studentController = new StudentController();
            String email = studentController.getEmail("rr");
            assertEquals("[email protected]",email);
            email = studentController.getEmail("jqj");
            assertEquals("[email protected]",email);
            email = studentController.getEmail("tony");
            assertEquals("[email protected]",email);
        } catch (Exception e) {
            e.printStackTrace ();
        } 
    } 
}

answer interface parameters used InvocationOnMock

invocation.getArguments (); (. 1 ) 
invocation.callRealMethod (); ( 2 ) 
invocation.getMethod (); ( . 3 ) 
invocation.getMock (); ( . 4 ) 
( . 1 ) acquires mock passed into the reference method 
( 2 ) that is the real way to get the call to mock the interface 
( 3 ) get so mock method is called 
( 4) to obtain the object after being mock

This, in use on mock Answer's presentation, interested students can knock themselves from top to bottom yourself again.

Guess you like

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