Mock Final

mockfinal相对来说就比较简单了,使用powermock来测试使用final修饰的method或class,比较简单,接口调用部分,还是service调用dao。

对于接口及场景这里就不细说了,特别简单。

service层

具体代码示例如下:

package com.rongrong.powermock.mockfinal;

/**
* @author rongrong
* @version 1.0
* @date 2019/11/27 21:29
*/
public class StudentFinalService { private StudentFinalDao studentFinalDao; public StudentFinalService(StudentFinalDao studentFinalDao) {
this.studentFinalDao = studentFinalDao;
} public void createStudent(Student student) {
studentFinalDao.isInsert(student);
}
}

dao层

为了模拟测试,我在dao层的类加了一个final关键字进行修饰,也就是这个类不允许被继承了。

具体代码如下:

package com.rongrong.powermock.mockfinal;

/**
* @author rongrong
* @version 1.0
* @date 2019/11/27 21:20
*/
final public class StudentFinalDao { public Boolean isInsert(Student student){
throw new UnsupportedOperationException();
}
}

进行单元测试

为了区分powermock与Easymock的区别,我们先采用EasyMock测试,这里先忽略EasyMock的用法,有兴趣的同学可自行去尝试学习。

使用EasyMock进行测试

具体代码示例如下:

    @Test
public void testStudentFinalServiceWithEasyMock(){
//mock对象
StudentFinalDao studentFinalDao = EasyMock.createMock(StudentFinalDao.class);
Student student = new Student();
//mock调用,默认返回成功
EasyMock.expect(studentFinalDao.isInsert(student)).andReturn(true);
EasyMock.replay(studentFinalDao);
StudentFinalService studentFinalService = new StudentFinalService(studentFinalDao);
studentFinalService.createStudent(student);
EasyMock.verify(studentFinalDao);
}

我们先来运行下这个单元测试,会发现运行报错,具体如下图显示:

很明显由于有final关键字修饰后,导致不能让测试成功,我们可以删除final关键再来测试一下,结果发现,测试通过。

使用PowerMock进行测试

具体代码示例如下:

package com.rongrong.powermock.mockfinal;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner; /**
* @author rongrong
* @version 1.0
* @date 2019/11/27 22:10
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(StudentFinalDao.class)
public class TestStudentFinalService { @Test
public void testStudentFinalServiceWithPowerMock(){
StudentFinalDao studentFinalDao = PowerMockito.mock(StudentFinalDao.class);
Student student = new Student();
PowerMockito.when(studentFinalDao.isInsert(student)).thenReturn(true);
StudentFinalService studentFinalService = new StudentFinalService(studentFinalDao);
studentFinalService.createStudent(student);
Mockito.verify(studentFinalDao).isInsert(student);
}
}

运行上面的单元测试时,会发现运行通过!!

最新文章

  1. PHP JSON数组与对象的理解
  2. VMware10.06精简版安装后台运行
  3. Twitter Snowflake 的Java实现
  4. error while loading shared libraries: /usr/lib64/libc.so.6: invalid ELF header
  5. [转]单例模式——C++实现自动释放单例类的实例
  6. 【笨嘴拙舌WINDOWS】GDI映射方式
  7. <PHP>字符串处理代码
  8. JS帮你计算属相
  9. ACM POJ 2192 Zipper
  10. C#+QI的例子
  11. eclipse查看类源码出现failed to create the part's controls的解决方法
  12. display、visibility、visible区别
  13. Ubuntu如何安装vncserver
  14. 萌新 学习 vuex
  15. LAMP架构(三)
  16. Jenkins-job迁移
  17. Java java.lang.SecurityException: Prohibited package name
  18. java list集合运算
  19. 【Laravel5.5】 Laravel 在views中加载公共页面怎么实现
  20. webp格式

热门文章

  1. python 读取文件夹中的文件内容
  2. Linux命令比较文件内容
  3. Spring(五)Spring缓存机制与Redis的结合
  4. 设计模式(十一)Composite模式
  5. C#控件的简单应用
  6. Python语法入门02
  7. $.ajax.html
  8. Linux 提示符格式及颜色
  9. 身份证号码验证算法(php和js实现)
  10. 【C#多线程】2.线程池简述+两种传统的异步模式