PowerMock學習(九)之Mock Answer的使用


關於Mock Answer

上一篇文章,有介紹過關於Arguments Matche的使用,其實 Answer的作用與其比較類似,但是它比 Arguments Matcher 更加強大。

Arguments Matche

即傳入不同的參數,返回不同的結果,重在入參的判斷,在入參重寫方法去判斷

Answer

見名知意,即返回不同的結果,但是根據傳入參數去判斷,在返回處重寫方法去判斷,返回結果

模擬場景

根據學生名字查找郵箱,controller調service層

service層

具體代碼示例如下:
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層

具體代碼示例如下:

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);
    }
}

 

上面的代碼的業務代碼比較簡單了,下面再來進行測試

具體示例代碼如下:

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 "rongrong@qq.com";
                } else if ("jqj".equals(arg)) {
                    return "jiuqujian@qq.com";
                }
                throw new NullPointerException();
            }
        });
        try {
            PowerMockito.whenNew(StudentAnswerService.class).withAnyArguments().thenReturn(studentAnswerService);
            StudentController studentController = new StudentController();
            String email = studentController.getEmail("rr");
            assertEquals("rongrong@qq.com",email);
            email = studentController.getEmail("jqj");
            assertEquals("jiuqujian@qq.com",email);
            email = studentController.getEmail("tony");
            assertEquals("jiuqujian@qq.com",email);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

answer 接口中參數 InvocationOnMock使用

invocation.getArguments();(1)
invocation.callRealMethod();(2)
invocation.getMethod();(3)
invocation.getMock();(4)
(1)獲取 mock 方法中傳遞的入參
(2)獲取是那個真實的方法調用了該 mock 接口
(3)獲取是那么 mock 方法被調用了
(4)獲取被 mock 之后的對象

到此,關於mock中 Answer的使用介紹完,有興趣的同學可以自己從上到下自己敲一遍。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM