关于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的使用介绍完,有兴趣的同学可以自己从上到下自己敲一遍。

最新文章

  1. Lightoj 题目1422 - Halloween Costumes(区间DP)
  2. Android加载SO库UnsatisfiedLinkError错误的原因及解决方案
  3. WCF 无法激活服务,因为它不支持 ASP.NET 兼容性。已为此应用程序启用了 ASP.NET 兼容性
  4. 7、网页制作Dreamweaver(悬浮动态分层导航)
  5. POJ 3090 (欧拉函数) Visible Lattice Points
  6. mysql主配置文件my.cnf详细说明
  7. Angular学习笔记(2)——TODO小应用
  8. JS 阻止整个网页的内容被选中
  9. FATFS外置UNICODE GBK双向转换码表(转)
  10. ambari
  11. pwnable.kr col
  12. nodejs-日志组件log4js的使用方法
  13. D3.js v5 Tutorials
  14. SqlServer中的查询简单总结
  15. git branch 命令查看分支、删除远程分支、本地分支
  16. flask 操作数据库(分类)
  17. JS--理解参数,argument,重载
  18. Android下基于SDL的位图渲染(二)理论篇
  19. sonarqube 代码检查
  20. 时间操作(JavaScript版)—页面显示格式:年月日 上午下午 时分秒 星期

热门文章

  1. incompatible implicit declaration of built-in function &#39;fabs&#39;
  2. linux 安装swoole
  3. vue2.0 与 vue3.0 配置的区别
  4. 详解PHP中的三大经典模式
  5. CVE-2019-17671:Wordpress未授权访问漏洞复现
  6. js中几种继承实现
  7. 1、Hibernate-入门
  8. 智和网管平台SugarNMS助力网络安全运维等保2.0建设
  9. Golang 基础学习笔记(2)| 如何安装Go工具
  10. 腾讯开源进入爆发期,Plato助推十亿级节点图计算进入分钟级时代