unit testing on the rocks!
Why mock?
Isolation
Example    AccountController                          HellotxtManagement+addAccount+deleteAccount                         ...
Example    AccountController                          HellotxtManagement+addAccount+deleteAccount                         ...
The no-so-good way                              HellotxtManagement                             ...    HellotxtManagementMo...
The no-so-good way                              HellotxtManagement                             ...    HellotxtManagementMo...
The no-so-good way                              HellotxtManagement                             ...    HellotxtManagementMo...
The mockito way
Test Workflow1. Create Mock2. Stub3. Invoke4. Verify5. Assert expectations
Creating a Mockimport static org.mockito.Mockito.*;...// You can mock concrete classes, not only interfacesLinkedList mock...
Stubbing// stubbingwhen(mockedList.get(0)).thenReturn("first");when(mockedList.get(1)).thenThrow(new RuntimeException());
What’s going on?// following prints "first"System.out.println(mockedList.get(0));// following throws runtime exceptionSyst...
Verify// Although it is possible to verify a stubbed invocation, usually its// just redundant// If your code cares what ge...
What about void       methods?• Often void methods you just verify them.         //create my mock and inject it to my unit...
What about void            methods?• Although you can also Stub them to throw     an exception. @Test(expected = HorribleE...
Argument Matcherswhen(hellotxtManagementMock.addAccount(Mockito.any(Session.class), Mockito.anyString(),	   	   	   	   	 ...
Custom Argument                  MatcherMockito.when(hellotxtManagementMock.getAuthenticationInfo(Mockito.argThat(new IsVa...
Argument Capture    ArgumentCaptor<Session> sessionCaptor = ArgumentCaptor.forClass(Session.class);                       ...
Basic Unit Testing with Mockito
Basic Unit Testing with Mockito
Upcoming SlideShare
Loading in...5
×

Basic Unit Testing with Mockito

9,361

Published on

Introduction of basic features of Mockito.

Published in: Technology, Education
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
9,361
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
223
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 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());
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×