Mock单元测试模板

xiaoxiao2021-02-28  91

验证调用测试类自己的方法,使用spy方法 @Test public void should_resetInfo_when_auto() throws Exception { //given Presenter presenter=spy(Presenter.class); //when presenter.auto("000222"); //then verify(presenter).resetInfo("000222"); } 验证是否正确调用其他类方法 , 使用verify @Test public void should_setInfo_when_resetInfo() throws Exception { //given Presenter presenter=new Presenter() ; View view = mock(View.class); //when presenter.resetInfo("000222"); //then verify(view).setInfo("000222"); } 验证不会调用其他类方法,使用never @Test public void should_setInfo_when_resetInfo() throws Exception { //given Presenter presenter=new Presenter() ; View view = mock(View.class); //when presenter.judgeStep(2); //then verify(view, never()).showEmpty(); } 只真实调用该方法,不调用其他方法如 initData() 等, 使用PowerMockito.doCallRealMethod()调用真实的方法, 测试类前添加@RunWith(PowerMockRunner.class) @Test public void should_setInfo_when_resetInfo() throws Exception { //given Presenter presenter=mock(Presenter.class); View view = mock(View.class); //when PowerMockito.doCallRealMethod().when(presenter).resetInfo("000222"); presenter.resetInfo("000222"); //then verify(view).setInfo("000222"); } 验证方法时,需要模拟其他方法调用的返回值,使用Mockito.when(MockObject.method(Mockito.any(Object))).thenReturn(object); @Test public void should_setText_when_setText() throws Exception { //given Presenter presenter=spy(Presenter.class); View view = mock(View.class); when(view .getDay(anyString())).thenReturn(7); //when presenter.setText(); //then verify(view).setInfo("000222"); } 验证网络请求onSuccess回调方法,使用ArgumentCaptor  PS:验证调用单例类的方法,需使用PowerMockito.mockStatic(Method) @Test public void should_onSuccess_when_doRequest() throws Exception { //given repository = mock(Repository.class); PowerMockito.mockStatic(Repository.class); when(Repository.getInstance()).thenReturn(repository); Presenter presenter=spy(Presenter.class); View view = mock(View.class); String username = "147260"; String password = "123456"; //when presenter.doRequest(username, password); //then ArgumentCaptor<BaseHttpRequestListener> captor = ArgumentCaptor.forClass(BaseHttpRequestListener.class); verify(repository).doRequest(eq("147260"), eq("123456"), captor.capture()); captor.getValue().onSuccess(result); verify(view).loginSuccess(); } 验证方法抛出异常  @Test(expected = Exception.class) @Test(expected = NullPointerException.class) public void should_throw_Exception_when_setText() throws Exception { //given Presenter presenter=spy(Presenter.class); View view = mock(View.class); //when presenter.setText(); //then verify(view).setInfo("000222"); } 模拟调用方法时的参数匹配  when(mockedList.get(anyString())).thenReturn(element); anyString()匹配任何int参数,这表示参数为任何值,均返回 7 @Test public void should_setDay_when_setDay() throws Exception { //given Presenter presenter=spy(Presenter.class); View view = mock(View.class); when(view .getDay(anyString())).thenReturn(7); //when presenter.setDay(); //then verify(view).setDay(7); } 模拟方法调用次数  time调用次数: verify(mock,times(n)).method();  atLeast至少调用的次数: verify(mock,atLeast(n)).method();  atLeastOnce最少调用一次: verify(mock,atLeastOnce()).method();  atMost最多调用次数: verify(mock,atMost(n)).method(); @Test public void should_throw_Exception_when_setText() throws Exception { //given Presenter presenter=spy(Presenter.class); View view = mock(View.class); //when presenter.setDate(); //then verify(view,times(2)).setDate(7); }
转载请注明原文地址: https://www.6miu.com/read-38869.html

最新回复(0)