Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Basic Unit Testing with Mockito

10,928 views

Published on

Introduction of basic features of Mockito.

Published in: Technology, Education

Basic Unit Testing with Mockito

  1. 1. unit testing on the rocks!
  2. 2. Why mock?
  3. 3. Isolation
  4. 4. Example AccountController HellotxtManagement+addAccount+deleteAccount ......
  5. 5. Example AccountController HellotxtManagement+addAccount+deleteAccount ......
  6. 6. The no-so-good way HellotxtManagement ... HellotxtManagementMock void addAccount(){ //do what I want } ..
  7. 7. The no-so-good way HellotxtManagement ... HellotxtManagementMock HellotxtManagementMock2 void addAccount(){ void addAccount(){ //do what I want //now do this } } .. ..
  8. 8. The no-so-good way HellotxtManagement ... HellotxtManagementMock HellotxtManagementMock2 HellotxtManagementMock3 void addAccount(){ void addAccount(){ void addAccount(){ //do what I want //now do this //throw exception } } } .. .. ..
  9. 9. The mockito way
  10. 10. Test Workflow1. Create Mock2. Stub3. Invoke4. Verify5. Assert expectations
  11. 11. Creating a Mockimport static org.mockito.Mockito.*;...// You can mock concrete classes, not only interfacesLinkedList mockedList = mock(LinkedList.class);
  12. 12. Stubbing// stubbingwhen(mockedList.get(0)).thenReturn("first");when(mockedList.get(1)).thenThrow(new RuntimeException());
  13. 13. What’s going on?// following prints "first"System.out.println(mockedList.get(0));// following throws runtime exceptionSystem.out.println(mockedList.get(1));// following prints "null" because get(999) was not stubbedSystem.out.println(mockedList.get(999));
  14. 14. Verify// Although it is possible to verify a stubbed invocation, usually its// just redundant// If your code cares what get(0) returns then something else breaks// (often before even verify() gets executed).// If your code doesnt care what get(0) returns then it should not be// stubbed. Not convinced? See here.verify(mockedList).get(0);
  15. 15. What about void methods?• Often void methods you just verify them. //create my mock and inject it to my unit under test DB mockedDb = mock(DB.class); MyThing unitUnderTest = new MyThing(mockedDb); //invoke what we want to test unitUnderTest.doSothing(); //verify we didnt forget to save my stuff. verify(mockedDb).save();
  16. 16. What about void methods?• Although you can also Stub them to throw an exception. @Test(expected = HorribleException.class) public void test3() throws Exception { // create my mock and inject it to my unit under test Oracle oracleMock = mock(Oracle.class); MyThing unitUnderTest = new MyThing(oracleMock); String paradox = "This statement is false"; // stub doThrow(new HorribleException()).when(oracleMock).thinkAbout(paradox); unitUnderTest.askTheOracle(paradox); }
  17. 17. Argument Matcherswhen(hellotxtManagementMock.addAccount(Mockito.any(Session.class), Mockito.anyString(), Mockito.anyMap(), Mockito.anyString())).thenReturn(account);
  18. 18. Custom Argument MatcherMockito.when(hellotxtManagementMock.getAuthenticationInfo(Mockito.argThat(new IsValidSession()), Mockito.eq(Networks.FACEBOOK))).thenReturn(expectedResult); ...private class IsValidSession extends ArgumentMatcher<Session> { @Override public boolean matches(Object argument) { return argument instanceof Session && ((Session) argument).getSessionId().equals(TestValues.VALID_SESSION_ID); }}
  19. 19. Argument Capture ArgumentCaptor<Session> sessionCaptor = ArgumentCaptor.forClass(Session.class); ... ModelAndView result = accountController.addAccount(json.toString(), sessionId, deviceType, deviceId, networkId); ...verify(hellotxtManagementMock).addAccount(sessionCaptor.capture(), Mockito.eq(networkId), Mockito.eq(parameters), Mockito.eq(redirectUrl)); ... assertEquals(sessionId, sessionCaptor.getValue().getSessionId()); assertEquals(deviceType, sessionCaptor.getValue().getDevice().getType().name()); assertEquals(deviceId, sessionCaptor.getValue().getDevice().getUid());

×