The document provides an overview of testing Java code using test-driven development (TDD). It discusses the basics of TDD, including writing tests before code, running tests frequently during development, and using tests to drive code design. It also covers best practices for unit testing like testing individual units of code in isolation using mock objects, verifying method calls and arguments, and testing exceptional behavior. The document emphasizes writing testable code, using testing frameworks to their full capabilities, and having thorough test coverage of all requirements and use cases.
1. Testing your code
For Java developers
Anna Khasanova
Anna.Khasanova@exigenservices.com
27 March 2013
Exigen Services confidential Exigen Services confidential
2. Agenda
• Testing basics
• TDD approach
• Testing in action
• Best practices
Exigen Services confidential 2
3. What is a test? Why do we need it?
• What is test?
• Why do we need testing?
• Automation
Exigen Services confidential 3
4. Automatic testing
Write once run often:
• Write test once
• Run frequently:
• After each change
• Continuous integration
• No human input
Exigen Services confidential 4
5. What is Test Driven Development?
TDD THEORY
Exigen Services confidential 5
6. Test Driven Development
• Software development approach
• Test before code
• Test - code requirements
Exigen Services confidential 6
8. New change request?
Design
Write Test succeeds
test
Test fails
Run test
Write Test fails
code
Whole story covered
Test
succeeds
Run test
Refactor Test fails
code
All tests succeed
Test
succeeds
Run test
Exigen Services confidential 8
9. Bugfix?
• Get bug report
• Turn it into a test
• Test should fail
• Fix bug
• Test should pass
Exigen Services confidential 9
10. How should I work?
How to write tests?
THE PRACTICE
Exigen Services confidential 10
11. What is Unit Test?
• Automated test for
• One business unit
• One business case
• Isolated
Exigen Services confidential 11
12. Unit Test is
• Small
• Fast
• Self documented
Exigen Services confidential 12
13. 3 parts of Unit-test
unitTest() {
// set preconditions: “arrange”
// call tested method: “act”
// assert results are as expected: “assert”
}
Exigen Services confidential 13
20. Example
PicturesService
+getSquarePictures()
Repository
+getAllPictures()
Exigen Services confidential 20
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. 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. 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. 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. 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. 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. 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