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.

Testing your code

685 views

Published on

by Anna Khasanova

  • Be the first to comment

  • Be the first to like this

Testing your code

  1. 1. Testing your code For Java developers Anna Khasanova Anna.Khasanova@exigenservices.com27 March 2013 Exigen Services confidential Exigen Services confidential
  2. 2. Agenda• Testing basics• TDD approach• Testing in action• Best practices Exigen Services confidential 2
  3. 3. What is a test? Why do we need it?• What is test?• Why do we need testing?• Automation Exigen Services confidential 3
  4. 4. Automatic testingWrite once run often:• Write test once• Run frequently: • After each change • Continuous integration• No human input Exigen Services confidential 4
  5. 5. What is Test Driven Development? TDD THEORYExigen Services confidential 5
  6. 6. Test Driven Development• Software development approach• Test before code• Test - code requirements Exigen Services confidential 6
  7. 7. Advantages• Increase requirements quality• Increase code quality• No unnecessary code Exigen Services confidential 7
  8. 8. New change request?Design Write Test succeeds test Test failsRun test Write Test fails codeWhole story covered Test succeeds Run test Refactor Test fails code All tests succeed Test succeeds Run test Exigen Services confidential 8
  9. 9. Bugfix?• Get bug report• Turn it into a test• Test should fail• Fix bug• Test should pass Exigen Services confidential 9
  10. 10. How should I work? How to write tests? THE PRACTICEExigen Services confidential 10
  11. 11. What is Unit Test?• Automated test for • One business unit • One business case• Isolated Exigen Services confidential 11
  12. 12. Unit Test is• Small• Fast• Self documented Exigen Services confidential 12
  13. 13. 3 parts of Unit-testunitTest() { // set preconditions: “arrange” // call tested method: “act” // assert results are as expected: “assert”} Exigen Services confidential 13
  14. 14. Unit-Test libraries• xUnit – collective naming: dbUnit, htmlUnit, qUnit, etc.• Java: jUnit• Java: TestNG• Javascript: jasmine Exigen Services confidential 14
  15. 15. Example of TDDTelephone field validator: • Allowed characters: • numbers: [0-9] • minus, space: “-” , “ ” • Length: 10 digits (ignore spaces and minuses) • Leading and trailing spaces are allowed Exigen Services confidential 15
  16. 16. Mock all dependencies REAL LIFEExigen Services confidential 16
  17. 17. We have a problem!• Method uses web service• or some other class/function• We don’t want to test it Exigen Services confidential 17
  18. 18. Mock• Fake implementation• We set what mock returns• Useful for unit-testing Exigen Services confidential 18
  19. 19. Mock libraries• Mockito• EasyMock• jMock Exigen Services confidential 19
  20. 20. Example PicturesService +getSquarePictures() Repository +getAllPictures() Exigen Services confidential 20
  21. 21. Example@Test public void testGetSquarePicturesEmptyResult() { PicturesService testedService = new PicturesService(); Repository repository = mock(Repository.class); //create fake Repository, returning empty list of pictures when(repository.getAllPictures()) .thenReturn(Collections.<Picture>emptyList()); testedService.setRepository(repository); Set<Picture> result = testedService.getSquarePictures(); assertTrue(result.isEmpty()); } Exigen Services confidential 21
  22. 22. How to verify external calls?• What to do if: • Tested method should call externals • We need to ensure that it was called?• Mocks scenario verification Exigen Services confidential 22
  23. 23. Example@Test public void testDeleteSquarePicturesEmptyResult() { PicturesService testedService = new PicturesService(); Repository repository = mock(Repository.class); Mockito.when(repository.getAllPictures()) .thenReturn(Collections.<Picture>emptyList()); testedService.setRepository(repository); testedService.deleteSquarePictures(); verify(repository, never()).deletePictures(any()); } Exigen Services confidential 23
  24. 24. How to verify arguments?• What to do if: • Mocked method is called with parameters • We need to test passed parameters?• Argument Captor• Matchers Exigen Services confidential 24
  25. 25. Example@Test public void testDeleteSquarePictures_captor() { … ArgumentCaptor<Iterable> captor = ArgumentCaptor.forClass(Iterable.class); verify(repository).deletePictures(captor.capture()); Iterable<Picture> result = captor.getValue(); … } Exigen Services confidential 25
  26. 26. How to verify exceptional cases?• What to do if: • Tested method should throw exception in some case • We need to test this case?• Expected exceptions @Test(expected = IllegalArgumentException.class) public void testFactorialNegative() { Factorial.factorial(-2); } Exigen Services confidential 26
  27. 27. How to verify exceptional cases?• What to do if: • We need to test time of execution?• Timeout parameter @Test(timeout = 1000) public void infinity() { while (true) ; } Exigen Services confidential 27
  28. 28. OTHER USEFUL FEATURESExigen Services confidential 28
  29. 29. Other features: matchers Matchers• when(…), verify(…)• assertThat(…)• See hamcrest library• Write your own Exigen Services confidential 29
  30. 30. Other features: runners Test runners• @RunWith • Spring: SpringJUnit4ClassRunner • Parameterized • Any other Exigen Services confidential 30
  31. 31. To sum this all up… SUMMARY. GUIDELINES.Exigen Services confidential 31
  32. 32. Coverage• Coverage: what needs to be covered• Setters-getters Exigen Services confidential 32
  33. 33. Above all1. Understand requirements2. Commit your understanding • Java docs • Any other Exigen Services confidential 33
  34. 34. Unit test• One test – one case• Each use case – covered• Unit test != trash Exigen Services confidential 34
  35. 35. Write testable code• Statics are evil• Extract interfaces• One method must not do “everything” Exigen Services confidential 35
  36. 36. Use framework’s features• Parameterized tests• Expected exceptions, timeouts• Mock objects scenarios• Matchers Exigen Services confidential 36
  37. 37. Finally• Automatic testing – difficult to start, but…• Don’t give up! Exigen Services confidential 37
  38. 38. Your turn QUESTIONS?Exigen Services confidential 38

×