User { public void login(final LoginInterface loginInterface) { if (webService.isNetworkOffline()) { loginInterface.onLoginFailed(); return; } Response response = new Response() { @Override public void onRequestCompleted(boolean isSuccess, String data) { if (isSuccess) { loginInterface.onLoginSuccess(); } else { loginInterface.onLoginFailed(); } } }; webService.login(userId, password, response); } User(WebService webService, int userId, String password); void login(final LoginInterface loginInterface); void logout(); void sendMessage(String message); void getMessages();  }
@Test public void verifyInteractionParameters() throws Exception { User user = new User(mockWebService, USER_ID, PASSWORD); user.login(null); verify(mockWebService).login(anyInt(), anyString(), any(Response.class)); verify(mockWebService).login(eq(USER_ID), eq(PASSWORD), any(Response.class)); verify(mockWebService).login(not(eq(0)), not(eq("12345678")), any(Response.class)); verify(mockWebService).login(gt(0), startsWith("n1"), any(Response.class)); verify(mockWebService).login(leq(USER_ID), contains("c3"), isNotNull(Response.class)); verify(mockWebService).login(and(gt(0), lt(Integer.MAX_VALUE)), matches("n[1-9]{1}c[1-9]{1}[a-z]{3}"), isNotNull(Response.class)); }
@Test public void stubMethod() throws Exception { User user = new User(mockWebService, USER_ID, PASSWORD); when(mockWebService.isNetworkOffline()).thenReturn(true); user.login(mockLoginInterface); verify(mockWebService, never()).login(anyInt(), anyString(), any(Response.class)); }
@Test public void stubMethodMultiple() throws Exception { User user = new User(mockWebService, USER_ID, PASSWORD); when(mockWebService.isNetworkOffline()).thenReturn(true, false, true); user.login(mockLoginInterface); user.login(mockLoginInterface); user.login(mockLoginInterface); verify(mockWebService, times(1)).login(anyInt(), anyString(), any(Response.class)); }
@Test(expected = CustomException.class) public void stubMethodException() throws Exception { User user = new User(mockWebService, USER_ID, PASSWORD); when(mockWebService.isNetworkOffline()).thenThrow(CustomException.class); user.login(mockLoginInterface); }
@Test public void stubMethodAnswer() throws Exception { User user = new User(mockWebService, USER_ID, PASSWORD); when(mockWebService.isNetworkOffline()).then(new Answer<Boolean>() { int index = 0; @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { return index++ % 2 == 0; } }); user.login(mockLoginInterface); user.login(mockLoginInterface); user.login(mockLoginInterface); verify(mockWebService, times(1)).login(anyInt(), anyString(), any(Response.class)); }
@Test public void captureArguments() throws Exception { User user = new User(mockWebService, USER_ID, PASSWORD); user.login(mockLoginInterface); verify(mockWebService).login(anyInt(), anyString(), responseArgumentCaptor.capture()); Response response = responseArgumentCaptor.getValue(); response.onRequestCompleted(true, null); verify(mockLoginInterface).onLoginSuccess(); }