文档入口:
https://github.com/powermock/powermock/wiki
私有方法mock文档:
https://github.com/powermock/powermock/wiki/MockPrivate
静态方法mock文档
https://github.com/powermock/powermock/wiki/MockStatic
案例参考:
https://ehlxr.me/2017/07/25/use-introduction-of-powermock/
概述:
介绍基于Junit ,Mockito,PowerMockito 常用的测试方法,包括异常测试,私有方法测试,没有返回值的方法测试,基于mock或spy的测试;
其中包含常规方法mock,私有方法mock。
一 测试介绍
1)测试原则:
凡是需要验证的方法都可以写单元测试,证明预期行为(不区分 controller,service ,dao)
2)测试分类:
a)单元测试:测试方法的 “逻辑” 是否满足期望。
b)集成测试:集成各个依赖组件,测试整体流程是否能通。
3)关于依赖:
单元测试一般测试当前方法的逻辑,不测试被依赖的类方法或自己的私有方法。
4)Mock 和 Spy 生成类区别
Mock:生成的类,所有方法都不是真实的方法,而且返回值都是NULL; 通过when 指定行为。
Spy:生成的类,所有方法都是真实方法,返回值都是和真实方法一样的;通过when 来将某个真实的方法,替换为指定行为的执行。
5)参考
1)普通mockito mock
2)使用powerMock ,增强 静态方法,私有方法能力;
二 Demo 本程序依赖
Xml代码
4.0.0 x.test test 1.0-SNAPSHOT jar test http://maven.apache.org UTF-8 4.12 2.8.47 1.7.0 3.8.0 junit junit ${junit.version} test org.mockito mockito-core ${mockito.version} test org.powermock powermock-module-junit4 ${powermock.version} test org.powermock powermock-api-mockito2 ${powermock.version} test org.assertj assertj-core ${assertj.version} test com.google.guava guava 20.0 org.apache.maven.plugins maven-compiler-plugin
三 简单的测试
测试抛异常,无返回值的方法测试,私有方法测试
1)被测试类
Java代码
package x.test.simple; import com.google.common.base.Strings; /** * 测试demo class ** Created by shilei on 2017/7/10. */ public class HelloWho { private String who; public HelloWho(String who) { //条件路径 if (Strings.isNullOrEmpty(who)) { throw new IllegalArgumentException("who can not be null!"); } //赋值路径 this.who = who; } public void sayHello() { System.out.println(getHelloMessage()); } private String getHelloMessage() { return "Hello " + who; } }
2)测试类
Java代码
package x.test.simple; import org.assertj.core.api.Assertions; import org.junit.Test; import org.powermock.reflect.Whitebox; /** * 基础测试: ** 1 测试抛异常 * 2 测试无返回值 * 3 测试私有方法 */ public class HelloWhoTest { @Test(expected = IllegalArgumentException.class) public void test_construction_empty_who_throw_exception() { new HelloWho(""); } @Test(expected = IllegalArgumentException.class) public void test_construction_null_who_throw_exception() { new HelloWho(null); } /** * 无返回值的方法测试,查看其修改的成员变量是否成功 */ @Test public void test_construction_args_set_who() throws Exception { //预期结果 String expectWho = "A"; //执行被测试方法 HelloWho helloWho = new HelloWho(expectWho); //白盒,获取修改的成员变量 String actualWho = (String) Whitebox.getField(helloWho.getClass(), "who").get(helloWho); //断言 Assertions.assertThat(actualWho).isEqualTo(expectWho); } /** * 测试私有方法 */ @Test public void test_getHelloMessage() throws Exception { //准备方法参数 String who = "A"; //准备预期结构 String expextMessage = "Hello " + who; //执行被测试方法 HelloWho helloWho = new HelloWho(who); String actualMessage = Whitebox.invokeMethod(helloWho, "getHelloMessage"); //断言 Assertions.assertThat(actualMessage).isEqualTo(expextMessage); } }
四 基于Mock的测试
依赖mock,私有方法mock,验证方法是否被执行
1)被依赖类Dao
Java代码
package x.test.adv.dao.pojo; /** * 用户 ** Created by shilei on 2017/7/10. */ public class User { } package x.test.adv.dao; import x.test.adv.dao.pojo.User; /** * 用户dao * Created by shilei on 2017/7/10. */ public interface UserDao { /** * 保存 * * @param user 用户 * @return true : 保存成功 ; false : 用户已存在 */ boolean save(User user); }
2)待测试类Service
Java代码
package x.test.adv.service; import x.test.adv.dao.pojo.User; /** * 用户服务 ** Created by shilei on 2017/7/10. */ public interface UserService { /** * 保存 , 如果保存成功,记录日志 * * @param user user */ void save(User user); } package x.test.adv.service.impl; import x.test.adv.dao.UserDao; import x.test.adv.dao.pojo.User; import x.test.adv.service.UserService; /** * 带依赖的测试 *
* Created by shilei on 2017/7/10. */ public class UserServiceImpl implements UserService { private UserDao userDao; @Override public void save(User user) { //保存 boolean isSave = userDao.save(user); //写大数据日志 if (isSave) { writeLog(user); } } private void writeLog(User user) { System.out.println(user); } }
3)测试类
Java代码
package x.test.adv.service.impl; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import x.test.adv.dao.UserDao; import x.test.adv.dao.pojo.User; /** * 带依赖的测试: * 1 mock 依赖 * 2 mock 私有方法 */ @RunWith(PowerMockRunner.class) //mock 静态方法,私有方法,需要添加该注解,以便通知框架 @PrepareForTest(UserServiceImpl.class) public class UserServiceImplTest { //mock 依赖注入到该bean @InjectMocks private UserServiceImpl userServiceImpl; //mock 依赖 @Mock private UserDao userDao; //完成依赖注入 @Before public void setUp() { MockitoAnnotations.initMocks(this); } /** * 测试保存成功,写日志 */ @Test public void test_save_true_verify_writelog() throws Exception { // 侦查对象 UserServiceImpl spyUserService = PowerMockito.spy(userServiceImpl); //创建输入 User user = new User(); //打桩 // mock 依赖行为,等价于 BDDMockito.given(userDao.save(user)).willReturn(true); Mockito.when(userDao.save(user)).thenReturn(true); //mock 私有方法 PowerMockito.doNothing().when(spyUserService, "writeLog", user); //调用实际方法 spyUserService.save(user); //断言 writelog 被执行一次 PowerMockito.verifyPrivate(spyUserService, Mockito.times(1)).invoke("writeLog", user); } /** * 测试保存成功,写日志 */ @Test public void test_save_false_no_writelog() throws Exception{ // 侦查对象 UserServiceImpl spyUserService = PowerMockito.spy(userServiceImpl); //创建输入 User user = new User(); //打桩 // mock 依赖行为,等价于 BDDMockito.given(userDao.save(user)).willReturn(true); Mockito.when(userDao.save(user)).thenReturn(false); //mock 私有方法 PowerMockito.doNothing().when(spyUserService, "writeLog", user); //调用实际方法 spyUserService.save(user); //断言 writelog 被执行一次 PowerMockito.verifyPrivate(spyUserService, Mockito.times(0)).invoke("writeLog", user); } }