我是靠谱客的博主 自然流沙,最近开发中收集的这篇文章主要介绍java单元测试之Mock测试编写1. Mock一个对象2.添加预期返回结果3.校验方法入参4.校验调用次数,至少xx次,没有调用5.抛出异常6.校验调用顺序7. 校验调用关系8.,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

mock框架官方文档地址:https://javadoc.io/static/org.mockito/mockito-core/3.2.0/org/mockito/Mockito.html#verification

1. Mock一个对象

 //Let's import Mockito statically so that the code looks clearer
import static org.mockito.Mockito.*;
//mock creation
List mockedList = mock(List.class);
//using mock object
mockedList.add("one");
mockedList.clear();
//verification
verify(mockedList).add("one");
verify(mockedList).clear();

2.添加预期返回结果

 //You can mock concrete classes, not just interfaces
LinkedList mockedList = mock(LinkedList.class);
//stubbing 设置预期值,获取第一个值返回"first",获取第二个值时抛出异常
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenThrow(new RuntimeException());
//following prints "first"
System.out.println(mockedList.get(0));
//following throws runtime exception
System.out.println(mockedList.get(1));
//following prints "null" because get(999) was not stubbed
System.out.println(mockedList.get(999));

3.校验方法入参

 //stubbing using built-in anyInt() argument matcher
//设置预期返回结果 输入任何int类型的值都返回 "element"
when(mockedList.get(anyInt())).thenReturn("element");
//stubbing using custom matcher (let's say isValid() returns your own matcher implementation):
// 设置预期结果
调用contains()方法时返回 true
when(mockedList.contains(argThat(isValid()))).thenReturn(true);
//following prints "element"
System.out.println(mockedList.get(999));
//you can also verify using an argument matcher
verify(mockedList).get(anyInt());

如果对一个方法使用了参数校验,那么这个方法的所有参数都要进行校验


//above is correct - eq() is also an argument matcher
这个方法的所有参数都进行了校验,正确
verify(mock).someMethod(anyInt(), anyString(), eq("third argument"));
//above is incorrect - exception will be thrown because third argument is given without an argument matcher. 这个方法的第三个入参没有进行校验,不正确
verify(mock).someMethod(anyInt(), anyString(), "third argument");

4.校验调用次数,至少xx次,没有调用

 //using mock
mockedList.add("once");
mockedList.add("twice");
mockedList.add("twice");
mockedList.add("three times");
mockedList.add("three times");
mockedList.add("three times");
//following two verifications work exactly the same - times(1) is used by default
//默认判断调用一次
verify(mockedList).add("once");
verify(mockedList, times(1)).add("once");
//exact number of invocations verification
verify(mockedList, times(2)).add("twice");
verify(mockedList, times(3)).add("three times");
//verification using never(). never() is an alias to times(0)
verify(mockedList, never()).add("never happened");
//verification using atLeast()/atMost()
verify(mockedList, atMostOnce()).add("once");
verify(mockedList, atLeastOnce()).add("three times");
verify(mockedList, atLeast(2)).add("three times");
verify(mockedList, atMost(5)).add("three times");

5.抛出异常

doThrow(new RuntimeException()).when(mockedList).clear();
//following throws RuntimeException:
mockedList.clear();

6.校验调用顺序

// A. Single mock whose methods must be invoked in a particular order
List singleMock = mock(List.class);
//using a single mock
singleMock.add("was added first");
singleMock.add("was added second");
//create an inOrder verifier for a single mock
InOrder inOrder = inOrder(singleMock);
//following will make sure that add is first called with "was added first", then with "was added second"
inOrder.verify(singleMock).add("was added first");
inOrder.verify(singleMock).add("was added second");
// B. Multiple mocks that must be used in a particular order
List firstMock = mock(List.class);
List secondMock = mock(List.class);
//using mocks
firstMock.add("was called first");
secondMock.add("was called second");
//create inOrder object passing any mocks that need to be verified in order
InOrder inOrder = inOrder(firstMock, secondMock);
//following will make sure that firstMock was called before secondMock
inOrder.verify(firstMock).add("was called first");
inOrder.verify(secondMock).add("was called second");
// Oh, and A + B can be mixed together at will

7. 校验调用关系

 //using mocks - only mockOne is interacted
mockOne.add("one");
//ordinary verification
verify(mockOne).add("one");
//verify that method was never called on a mock
verify(mockOne, never()).add("two");
//verify that other mocks were not interacted
verifyZeroInteractions(mockTwo, mockThree);

8.

最后

以上就是自然流沙为你收集整理的java单元测试之Mock测试编写1. Mock一个对象2.添加预期返回结果3.校验方法入参4.校验调用次数,至少xx次,没有调用5.抛出异常6.校验调用顺序7. 校验调用关系8.的全部内容,希望文章能够帮你解决java单元测试之Mock测试编写1. Mock一个对象2.添加预期返回结果3.校验方法入参4.校验调用次数,至少xx次,没有调用5.抛出异常6.校验调用顺序7. 校验调用关系8.所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(40)

评论列表共有 0 条评论

立即
投稿
返回
顶部