相同点

spy和mock生成的对象不受spring管理

不同点

1.默认行为不同

对于未指定mock的方法,spy默认会调用真实的方法,有返回值的返回真实的返回值,而mock默认不执行,有返回值的,默认返回null

2.使用方式不同

Spy中用when...thenReturn私有方法总是被执行,预期是私有方法不应该执行,因为很有可能私有方法就会依赖真实的环境。
Spy中用doReturn..when才会不执行真实的方法。

mock中用 when...thenReturn 私有方法不会执行

3.代码统计覆盖率不同
@spy使用的真实的对象实例,调用的都是真实的方法,所以通过这种方式进行测试,在进行sonar覆盖率统计时统计出来是有覆盖率;
@mock出来的对象可能已经发生了变化,调用的方法都不是真实的,在进行sonar覆盖率统计时统计出来的Calculator类覆盖率为0.00%。

以下为测试代码部分


 

Calculator.java
class Calculator {
private int sumXX(int a, int b) {
System.out.println("sumXX");
return a + b;
} public int callSumXX(int a, int b) {
System.out.println("callSumXX");
return sumXX(a, b);
}
}
SpyAndMockTest.java
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; import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyInt; @RunWith(PowerMockRunner.class)
@PrepareForTest({Calculator.class})
public class SpyAndMockTest { @Test
public void testSumXXBySpy_Call_Private_Method() throws Exception {
Calculator cal= PowerMockito.spy(new Calculator());
PowerMockito.when(cal,"sumXX",anyInt(),anyInt()).thenReturn(2);
assertEquals(2, cal.callSumXX(1, 2));
} @Test
public void testSumXXBySpy_Not_Call_Private_Method() throws Exception {
Calculator cal= PowerMockito.spy(new Calculator());
PowerMockito.doReturn(2).when(cal,"sumXX",anyInt(),anyInt());
assertEquals(2, cal.callSumXX(1, 2));
} @Test
public void testSumXXByMock_Not_Call_Real_Method() throws Exception {
Calculator cal= PowerMockito.mock(Calculator.class);
PowerMockito.when(cal,"sumXX",anyInt(),anyInt()).thenReturn(2);
assertEquals(0, cal.callSumXX(1, 2));
}
@Test
public void testSumXXByMock_Call_Real_Method() throws Exception {
Calculator cal= PowerMockito.mock(Calculator.class);
PowerMockito.when(cal,"sumXX",anyInt(),anyInt()).thenReturn(2);
PowerMockito.when(cal.callSumXX(anyInt(),anyInt())).thenCallRealMethod();//指明callSumXX调用真实的方法
assertEquals(2, cal.callSumXX(1, 2));
}
}

附上 pom.xm文件依赖

      <dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>

  

最新文章

  1. js 判断字符串是否包含另外一个字符串
  2. asp.net关于页面不回发,不生成__doPostBack方法问题的完美解决方案
  3. com学习前提必看
  4. 关于JS异步加载方案
  5. 解决 Provider &#39;System.Data.SqlServerCe.3.5&#39; not installed. -摘自网络
  6. Discuz &lt;= 7.2 SQL注入漏洞详情
  7. make it clear how to use const in C++
  8. java+mysql中文乱码问题
  9. 4.mysql数据库创建,表中创建模具模板脚本,mysql_SQL99标准连接查询(恩,外部连接,全外连接,交叉连接)
  10. --save 和 --save-dev的区别
  11. 用java写一个用户登陆界面
  12. Hadoop2.7.3集群搭建
  13. 找出共同好友 - 数据挖掘 - Scala版
  14. Python 基础知识----流程控制
  15. Java生成数独函数
  16. tomcat 输入学习
  17. shell历史简介
  18. aspnet_regiis.exe -i 报 “此操作系统版本不支持此选项”
  19. js文件流下载通用方法
  20. Java基础知识强化之集合框架笔记80:HashMap的线程不安全性的体现

热门文章

  1. 4.Java JSON使用
  2. nginx详解(代理服务器的解释+nginx 在linux 下的安装+nginx.conf 中的配置解释)
  3. 008-centos6.5搭建web服务【nginx-tomcat8-jre8】
  4. 一百二十八:CMS系统之轮播图的编辑和删除功能
  5. router-link跳转页面传递参数及页面刷新方法
  6. 时间复杂度O(n)
  7. springmvc xml文件配置中使用系统环境变量
  8. Java中验证编码格式的一种方法
  9. SaCa CDC产品简介
  10. windows端口映射