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.

Introduction to Unit Testing (Part 2 of 2)

419 views

Published on

A presentation on introductory unit testing.

Published in: Software, Technology, Education
  • Be the first to comment

  • Be the first to like this

Introduction to Unit Testing (Part 2 of 2)

  1. 1. Dennis Byrne Introduction to Unit Test g Part 2 of 2
  2. 2. Agenda … ● mock() ● when() ● verify() ● ArgumentCaptor ● reset()
  3. 3. Encapsulation, Encapsulation, Encapsulation
  4. 4. Law of Demeter ● Each unit should have only limited knowledge about other units: only units "closely" related to the current unit. ● Each unit should only talk to its friends; don't talk to strangers. ● Only talk to your immediate friends.
  5. 5. Mockito.mock() @Test public void lawOfDemeterBroken(){ Person gnan = mock(Person.class); Pants pants = mock(Pants.class); Wallet wallet = mock(Wallet.class); Collection<Bill> bills = mock(Collection.class); Bill bill = mock(Bill.class); Iterator<Bill> iterator = mock(Iterator.class); // …
  6. 6. Mockito.when() // … when(gnan.getPants()).thenReturn(pants); when(pants.getWallet()).thenReturn(wallet); when(wallet.getBills()).thenReturn(bills); when(bills.iterator()).thenReturn(iterator); when(iterator.hasNext()).thenReturn(true); when(iterator.next()).thenReturn(bill); when(bill.getValue()).thenReturn(5);
  7. 7. Law of Demeter: broken // ... Person dennis = new Person(); borrow(gnan, dennis, 5); assertTrue(dennis.getPants().getWallet().getBills().contains(bill)); }
  8. 8. Law of Demeter: broken public static void borrow(Person from, Person to, int amount){ Iterator<Bill> iterator = from.getPants().getWallet().getBills().iterator(); while (iterator.hasNext()) { Bill bill = iterator.next(); if(bill.getValue() == amount){ iterator.remove(); to.getPants().getWallet().getBills().add(bill); break; } } }
  9. 9. Law of Demeter: fixed @Test public void lawOfDemeter(){ Person gnan = mock(Person.class); when(gnan.removeMoneyAmount(anyInt())).thenReturn(new Bill(5)); Person dennis = new Person(); dennis.borrow(5, gnan); assertEquals(5, dennis.getMoneyTotal()); }
  10. 10. “Test First” Running Total 1
  11. 11. Law of Demeter public String methodChain(){ StringBuilder builder = new StringBuilder(); builder.append("this") .append("doesn't") .append("break") .append("encapsulation"); return builder.toString(); }
  12. 12. Agenda … ● mock() ● when() ● verify() ● ArgumentCaptor ● reset()
  13. 13. Testing behavior/interactions @Test public void behaviorBad() { FederatedSearchServer searchServer = new FederatedSearchServer(){ private int callCount = 0; @Override public List<JSONObject> doPeopleNameSearch(String name) { if(callCount++ > 0) Assert.fail(); return super.doPeopleNameSearch(name); } }; VoltronServer voltron = new VoltronServer(searchServer); voltron.doPeopleNameSearch("conan"); voltron.doPeopleNameSearch("conan"); }
  14. 14. “Test First” Running Total 2
  15. 15. “Test First” Running Total 3
  16. 16. Mockito.verify @Test public void behavior(){ FederatedSearchServer searchServer = mock(FederatedSearchServer.class); when(searchServer.doPeopleNameSearch("conan")).thenReturn(new LinkedList<JSONObject>()); VoltronServer voltron = new VoltronServer(searchServer); voltron.doPeopleNameSearch("conan"); voltron.doPeopleNameSearch("conan"); verify(searchServer, times(1)).doPeopleNameSearch("conan"); // atLeast(n) anyString() // atMost(n) // never() }
  17. 17. “Test First” Running Total 4
  18. 18. Agenda … Mockito ● mock() ● when() ● verify() ● ArgumentCaptor ● reset()
  19. 19. Mockito - ArgumentCaptor @Test public void argumentCaptor() { FederatedSearchServer searchServer = mock(FederatedSearchServer.class); VoltronServer voltronServer = new VoltronServer(searchServer); voltronServer.doPeopleNameSearch("conan"); ArgumentCaptor<PeopleSearchRequest> captor = forClass(PeopleSearchRequest.class); verify(searchServer).doPeopleNameSearch(captor.capture()); PeopleSearchRequest passedToSearchServer = captor.getValue(); assertEquals("conan", passedToSearchServer.getSearchTerm()); }
  20. 20. Agenda … Mockito ● mock() ● when() ● verify() ● ArgumentCaptor ● reset()
  21. 21. TestNG vs. JUnit private FederatedSearchServer searchServer = mock(FederatedSearchServer.class); @Test public void searchWithStemming(){ VoltronServer voltronServer = new VoltronServer(searchServer); List<JSONObject> results = voltronServer.doPeopleNameSearch("con"); assertEquals(results.get(0).get("name"), "Conan O'Brian"); }
  22. 22. TestNG vs. JUnit @Test public void searchWithFullName(){ List<JSONObject> hits = new LinkedList<JSONObject>(); hits.add(new JSONObject("Conan O'Brian")); when(searchServer.doPeopleNameSearch(anyString())).thenReturn(hits); VoltronServer voltronServer = new VoltronServer(searchServer); List<JSONObject> results = voltronServer.doPeopleNameSearch("conan"); assertEquals(results.get(0).get("name"), "Conan O'Brian"); }
  23. 23. TestNG vs. JUnit @AfterMethod public void afterMethod(){ reset(this.searchServer); }
  24. 24. “Test First” Running Total 5
  25. 25. Agenda … Mockito ● mock() ● when() ● verify() ● ArgumentCaptor ● reset()

×