This action will force synchronization from yswift/LearnJava, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
import org.junit.Test;import java.util.LinkedList;import static org.mockito.ArgumentMatchers.anyInt;import static org.mockito.Mockito.*;/*** 使用mockito 框架进行测试,参见: 8 unit test.ppt*/public class DemoMockito {@Testpublic void mockObject() {// 模拟LinkedList 的一个对象LinkedList mockedList = mock(LinkedList.class);// 此时调用get方法,会返回null,因为还没有对方法调用的返回值做模拟System.out.println(mockedList.get(0));}@Testpublic void mockReturnValue() {LinkedList mockedList = mock(LinkedList.class);// 模拟获取第一个元素时,返回字符串first。 给特定的方法调用返回固定值在官方说法中称为stub。when(mockedList.get(0)).thenReturn("first");// 此时打印输出firstSystem.out.println(mockedList.get(0));}@Test(expected = RuntimeException.class)public void mockThrowException() {LinkedList mockedList = mock(LinkedList.class);// 模拟获取第二个元素时,抛出RuntimeExceptionwhen(mockedList.get(1)).thenThrow(new RuntimeException());// 此时将会抛出RuntimeExceptionSystem.out.println(mockedList.get(1));}@Testpublic void mockMethodArgs() {LinkedList mockedList = mock(LinkedList.class);// anyInt()匹配任何int参数,这意味着参数为任意值,其返回值均是elementwhen(mockedList.get(anyInt())).thenReturn("element");// 此时打印是elementSystem.out.println(mockedList.get(999));}@Testpublic void mockCallMethod() {LinkedList mockedList = mock(LinkedList.class);// 调用add一次mockedList.add("once");// 下面两个写法验证效果一样,均验证add方法是否被调用了一次verify(mockedList).add("once");verify(mockedList, times(1)).add("once");}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。