Mockito与PowerMock都是Java流行的一种Mock框架,使用Mock技术能让咱们隔离外部依赖以便对咱们本身的业务逻辑代码进行单元测试,在编写单元测试时,不须要再进行繁琐的初始化工做,在须要调用某一个接口时,直接模拟一个假方法,并任意指定方法的返回值。
Mockito的工做原理是经过建立依赖对象的proxy,全部的调用先通过proxy对象,proxy对象拦截了全部的请求再根据预设的返回值进行处理。PowerMock则在Mockito原有的基础上作了扩展,经过修改类字节码并使用自定义ClassLoader加载运行的方式来实现mock静态方法、final方法、private方法、系统类的功能。
从二者的项目结构中就能够看出,PowerMock直接依赖于Mockito,因此若是项目中已经导入了PowerMock包就不须要再单独导入Mockito包,若是二者同时导入还要当心PowerMock和Mockito不一样版本之间的兼容问题:java
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>2.23.0</version> <scope>test</scope> </dependency>
<dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>2.0.0-RC.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito2</artifactId> <version>2.0.0-RC.3</version> <scope>test</scope> </dependency>
Mockito通常经过建立mock或spy对象,并制定具体返回规则来实现模拟的功能,在调用完成后还能够进行方法调用验证以检验程序逻辑是否正确。mock和spy对象的区别是mock对象对于未指定处理规则的调用会按方法返回值类型返回该类型的默认值(如int、long则返回0,boolean则返回false,对象则返回null,void则什么都不作),而spy对象在未指定处理规则时则会直接调用真实方法。node
如下3个类是咱们的项目中须要用到的一些业务类:git
//实体类 public class Node { private int num; private String name; //如下忽略全部构造方法和get、set方法 } //本地负责实现具体业务的业务类 public class LocalServiceImpl implements ILocalService { //外部依赖 @Autowired private IRemoteService remoteService; //具体业务处理方法 @Override public Node getRemoteNode(int num) { return remoteService.getRemoteNode(num); } //如下忽略其余业务调用方法,在后面例子中补充 } //外部依赖业务类,由其余人实现,可能咱们的业务类写好了别人还没写好 public class RemoteServiceImpl implements IRemoteService { //外部类提供的一些业务方法 @Override public Node getRemoteNode(int num) { return new Node(num, "Node from remote service"); } //其余业务方法在后面例子中补充 }
下面是Mockito具体使用的一些示例:github
@RunWith(MockitoJUnitRunner.class) //让测试运行于Mockito环境 public class LocalServiceImplMockTest { @InjectMocks //此注解表示这个对象须要被注入mock对象 private LocalServiceImpl localService; @Mock //此注解会自动建立1个mock对象并注入到@InjectMocks对象中 private RemoteServiceImpl remoteService; //若是不使用上述注解,可使用@Before方法来手动进行mock对象的建立和注入,但会几行不少代码 /* private LocalServiceImpl localService; private RemoteServiceImpl remoteService; @Before public void setUp() throws Exception { localService = new LocalServiceImpl(); remoteService = Mockito.mock(RemoteServiceImpl.class); //建立Mock对象 Whitebox.setInternalState(localService, "remoteService", remoteService); //注入依赖对象 } */ @Test public void testMock() { Node target = new Node(1, "target"); //建立一个Node对象做为返回值 Mockito.when(remoteService.getRemoteNode(1)).thenReturn(target); //指定当remoteService.getRemoteNode(int)方法传入参数为1时返回target对象 Node result = localService.getRemoteNode(1); //调用咱们的业务方法,业务方法内部调用依赖对象方法 assertEquals(target, result); //能够断言咱们获得的返回值其实就是target对象 assertEquals(1, result.getNum()); //具体属性和咱们指定的返回值相同 assertEquals("target", result.getName()); //具体属性和咱们指定的返回值相同 Node result2 = localService.getRemoteNode(2); //未指定参数为2时对应的返回规则 assertNull(result2); //未指定时返回为null } }
@RunWith(MockitoJUnitRunner.class) public class LocalServiceImplSpyTest { @InjectMocks private LocalServiceImpl localService; @Spy //注意这里使用的是@Spy注解 private RemoteServiceImpl remoteService; //注意若是本身建立spy对象的话要这么写: /* remoteService = new RemoteServiceImpl(); //先建立一个具体实例 remoteService = Mockito.spy(remoteService); //再调用Mockito.spy(T)方法建立spy对象 */ @Test public void testSpy() { Node target = new Node(1, "target"); //建立一个Node对象做为返回值 Mockito.when(remoteService.getRemoteNode(1)).thenReturn(target); //指定当remoteService.getRemoteNode(int)方法传入参数为1时返回target对象 Node result = localService.getRemoteNode(1); //调用咱们的业务方法,业务方法内部调用依赖对象方法 assertEquals(target, result); //能够断言咱们获得的返回值其实就是target对象 assertEquals(1, result.getNum()); //具体属性和咱们指定的返回值相同 assertEquals("target", result.getName()); //具体属性和咱们指定的返回值相同 Node result2 = localService.getRemoteNode(2); //未指定参数为2时的调用规则,因此会直接调用真实对象,返回remote建立的节点 assertEquals(2, result2.getNum()); assertEquals("Node from remote service", result2.getName()); //remoteService建立Node对象时设置name属性为"Node from remote service" } }
@Test public void testAny() { Node target = new Node(1, "target"); when(remoteService.getRemoteNode(anyInt())).thenReturn(target); //静态导入Mockito.when和ArgumentMatchers.anyInt后能够简化代码提高可读性 Node result = localService.getRemoteNode(20); //上面指定了调用remoteService.getRemoteNode(int)时,无论传入什么参数都会返回target对象 assertEquals(target, result); //能够断言咱们获得的返回值其实就是target对象 assertEquals(1, result.getNum()); //具体属性和咱们指定的返回值相同 assertEquals("target", result.getName()); //具体属性和咱们指定的返回值相同 }
/** * 指定mock屡次调用返回值 */ @Test public void testMultipleReturn() { Node target1 = new Node(1, "target"); Node target2 = new Node(1, "target"); Node target3 = new Node(1, "target"); when(remoteService.getRemoteNode(anyInt())).thenReturn(target1).thenReturn(target2).thenReturn(target3); //第一次调用返回target一、第二次返回target二、第三次返回target3 Node result1 = localService.getRemoteNode(1); //第1次调用 assertEquals(target1, result1); Node result2 = localService.getRemoteNode(2); //第2次调用 assertEquals(target2, result2); Node result3 = localService.getRemoteNode(3); //第3次调用 assertEquals(target3, result3); }
//RemoteServiceImpl方法: @Override public Node getRemoteNode(String name) throws MockException { if (StringUtils.isEmpty(name)) { throw new MockException("name不能为空", name); } return new Node(name); } //LocalServiceImpl方法 @Override public Node getRemoteNode(String name) throws MockException { try { return remoteService.getRemoteNode(name); } catch (IllegalArgumentException e) { throw e; } } /** * 指定mock对象已声明异常抛出的方法抛出受检查异常 */ @Test public void testExceptionDeclare() { try { Node target = new Node(1, "target"); when(remoteService.getRemoteNode("name")).thenReturn(target).thenThrow(new MockException( "message", "exception")); //第一次调用正常返回,第二次则抛出一个Exception Node result1 = localService.getRemoteNode("name"); assertEquals(target, result1); //第一次调用正常返回 Node result2 = localService.getRemoteNode("name"); //第二次调用不会正常返回,会抛出异常 assertEquals(target, result2); } catch (MockException e) { assertEquals("exception", e.getName()); //验证是否返回指定异常内容 assertEquals("message", e.getMessage()); //验证是否返回指定异常内容 } } /** * 指定mock对象为声明异常抛出的方法抛出运行时异常 */ @Test public void testRuntimeException() { Node target = new Node(1, "target"); when(remoteService.getRemoteNode(1)).thenThrow(new RuntimeException("exception")); //指定调用时抛出一个运行时异常 try { Node result = localService.getRemoteNode(1); assertEquals(target, result); } catch (RuntimeException e) { assertEquals("exception", e.getMessage()); } } /** * 指定mock对象未声明异常抛出的方法抛出受检查异常,如下方法执行会报错 */ @Test public void testNotDefineCheckedException() { Node target = new Node(1, "target"); when(remoteService.getRemoteNode(1)).thenThrow(new IOException("io exception")); try { Node result = localService.getRemoteNode(1); assertEquals(target, result); } catch (Exception e) { assertEquals("io exception", e.getMessage()); } }
//RemoteServiceImpl方法: @Override public void doSometing() { System.out.println("remote service do something!"); } //LocalServiceImpl方法 @Override public void remoteDoSomething() { remoteService.doSometing(); } //注意void方法没有返回值,因此mock规则写法顺序不同 doNothing().when(remoteService).doSometing(); doThrow(new RuntimeException("exception")).when(remoteService).doSometing();
/** * 校验mock对象和方法的调用状况 * */ public void testVerify() { Node target = new Node(1, "target"); when(remoteService.getRemoteNode(anyInt())).thenReturn(target); verify(remoteService, never()).getRemoteNode(1); //mock方法未调用过 localService.getRemoteNode(1); Mockito.verify(remoteService, times(1)).getRemoteNode(anyInt()); //目前mock方法调用过1次 localService.getRemoteNode(2); verify(remoteService, times(2)).getRemoteNode(anyInt()); //目前mock方法调用过2次 verify(remoteService, times(1)).getRemoteNode(2); //目前mock方法参数为2只调用过1次 }
/** * 利用ArgumentCaptor捕获方法参数进行mock方法参数校验 */ @Test public void testCaptor() throws Exception { Node target = new Node(1, "target"); when(remoteService.getRemoteNode(anyString())).thenReturn(target); localService.getRemoteNode("name1"); localService.getRemoteNode("name2"); verify(remoteService, atLeastOnce()).getRemoteNode(localCaptor.capture()); //设置captor assertEquals("name2", localCaptor.getValue()); //获取最后一次调用的参数 List<String> list = localCaptor.getAllValues(); //按顺序获取全部传入的参数 assertEquals("name1", list.get(0)); assertEquals("name2", list.get(1)); }
/** * mock对象调用真实方法 */ @Test public void testCallRealMethod() { when(remoteService.getRemoteNode(anyInt())).thenCallRealMethod(); //设置调用真实方法 Node result = localService.getRemoteNode(1); assertEquals(1, result.getNum()); assertEquals("Node from remote service", result.getName()); }
//重置mock,清除全部的调用记录和返回规则 Mockito.reset(remoteService);
/** * 校验mock对象0调用和未被验证的调用 */ @Test(expected = NoInteractionsWanted.class) public void testInteraction() { verifyZeroInteractions(remoteService); //目前还未被调用过,执行不报错 Node target = new Node(1, "target"); when(remoteService.getRemoteNode(anyInt())).thenReturn(target); localService.getRemoteNode(1); localService.getRemoteNode(2); verify(remoteService, times(2)).getRemoteNode(anyInt()); // 参数1和2的两次调用都会被上面的anyInt()校验到,因此没有未被校验的调用了 verifyNoMoreInteractions(remoteService); reset(remoteService); localService.getRemoteNode(1); localService.getRemoteNode(2); verify(remoteService, times(1)).getRemoteNode(1); // 参数2的调用不会被上面的校验到,因此执行会抛异常 verifyNoMoreInteractions(remoteService); }
PowerMock的使用与Mockito有一些不一样,首先是测试类上的@RunWith注解须要修改成:api
@RunWith(PowerMockRunner.class)
第二是须要使用到@PrepareForTest注解(PrepareFotTest注解会修改传入参数类的字节码,经过修改字节码达到模拟final、static、私有方法、系统类等的功能),此注解可写在类上也可写在方法上:框架
@PrepareForTest(RemoteServiceImpl.class)
//LocalServiceImpl @Override public Node getLocalNode(int num, String name) { return new Node(num, name); } /** * mock new关键字 */ @Test @PrepareForTest(LocalServiceImpl.class) //PrepareForTest修改local类的字节码以覆盖new的功能 public void testNew() throws Exception { Node target = new Node(1, "target"); //当传入任意int且name属性为"name"时,new对象返回为target //当参数条件使用了any系列方法时,剩余的参数都得使用相应的模糊匹配规则,如eq("name")表明参数等于"name" //剩余还有isNull(), isNotNull(), isA()等方法 PowerMockito.whenNew(Node.class).withArguments(anyInt(), eq("name")).thenReturn(target); Node result = localService.getLocalNode(2, "name"); assertEquals(target, result); //返回值为target assertEquals(1, result.getNum()); assertEquals("target", result.getName()); //未指定name为"test"的返回值,默认返回null Node result2 = localService.getLocalNode(1, "test"); assertNull(result2); }
//RemoteServiceImpl @Override public final Node getFinalNode() { return new Node(1, "final node"); } /** * mock final方法 */ @Test @PrepareForTest(RemoteServiceImpl.class) //final方法在RemoteServiceImpl类中 public void testFinal() { Node target = new Node(2, "mock"); PowerMockito.when(remoteService.getFinalNode()).thenReturn(target); //指定返回值 Node result = remoteService.getFinalNode(); //直接调用final方法,返回mock后的值 assertEquals(target, result); //验证返回值 assertEquals(2, result.getNum()); assertEquals("mock", result.getName()); }
//Node public static Node getStaticNode() { return new Node(1, "static node"); } /** * mock static方法 */ @Test @PrepareForTest(Node.class) //static方法定义在Node类中 public void testStatic() { Node target = new Node(2, "mock"); PowerMockito.mockStatic(Node.class); //mock static方法前须要加这一句 PowerMockito.when(Node.getStaticNode()).thenReturn(target); //指定返回值 Node result = Node.getStaticNode(); //直接调用static方法,返回mock后的值 assertEquals(target, result); //验证返回值 assertEquals(2, result.getNum()); assertEquals("mock", result.getName()); }
//RemoteServiceImpl @Override public Node getPrivateNode() { return privateMethod(); } //RemoteServiceImpl private Node privateMethod() { return new Node(1, "private node"); } /** * mock 私有方法 */ @Test @PrepareForTest(RemoteServiceImpl.class) //private方法定义在RemoteServiceImpl类中 public void testPrivate() throws Exception { Node target = new Node(2, "mock"); //按照真实代码调用privateMethod方法 PowerMockito.when(remoteService.getPrivateNode()).thenCallRealMethod(); //私有方法没法访问,相似反射传递方法名和参数,此处无参数故未传 PowerMockito.when(remoteService, "privateMethod").thenReturn(target); Node result = remoteService.getPrivateNode(); assertEquals(target, result); //验证返回值 assertEquals(2, result.getNum()); assertEquals("mock", result.getName()); }
//RemoteServiceImpl @Override public Node getSystemPropertyNode() { return new Node(System.getProperty("abc")); } /** * mock 系统类方法 */ @Test @PrepareForTest(RemoteServiceImpl.class) //相似new关键字,系统类方法的调用在类RemoteServiceImpl中,因此这里填的是RemoteServiceImpl public void testSystem() { PowerMockito.mockStatic(System.class); //调用的是系统类的静态方法,因此要加这一句 PowerMockito.when(System.getProperty("abc")).thenReturn("mock"); //设置System.getProperty("abc")返回"mock" PowerMockito.when(remoteService.getSystemPropertyNode()).thenCallRealMethod(); //设置mock对象调用实际方法 Node result = remoteService.getSystemPropertyNode(); //按代码会返回一个name属性为"mock"的对象 assertEquals(0, result.getNum()); //int默认值为0 assertEquals("mock", result.getName()); //remoteService对象中调用System.getProperty("abc")返回的是上面设置的"mock" }
项目代码已上传至GitHub:MockDemomaven