Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Automated Testing In Java
1. Automated Testing in Java By Franz See [email_address] http://twitter.com/franz_see
2. Problems? Regression Too much time Debugging Waiting for your build to finish Documentation that lies!
3. What is it? public class Image { public Image(String path) {...}; public MimeType getMimeType() {...}; public Dimension getDimension() {...}; } public class ImageTest extends TestCase { public void testGetMimeType() { Image jpegImage = new Image(“dummy.jpeg”); MimeType mimeType = jpegImage.getMimeType(); assertEquals(MimeType.JPEG, mimeType); } public void testGetDimension() { Image jpegImage = new Image(“dummy.jpeg”); Dimension dimension = jpegImage.getDimension(); assertEquals(new Dimension(400,600), dimension); } }
4. The Parts (part1) public class ImageTest extends TestCase { public void testGetMimeType() { // setup Image jpegImage = new Image(“dummy.jpeg”); // exercise MimeType mimeType = jpegImage.getMimeType(); // verify assertEquals(MimeType.JPEG, mimeType); } // implict teardown public void testGetDimension() { Image jpegImage = new Image(“dummy.jpeg”); Dimension dimension = jpegImage.getDimension(); assertEquals(new Dimension(400,600), dimension); } }
5. The Parts (part2) public class ImageTest extends TestCase { private Image jpegImage; protected void setUp() { super.setUp(); jpegImage = new Image(”dummy.jpeg”); } protected void tearDown() { jpegImage = null; super.tearDown(); } public void testGetMimeType() { MimeType mimeType = jpegImage.getMimeType(); assertEquals(MimeType.JPEG, mimeType); } public void testGetDimension() { Dimension dimension = jpegImage.getDimension(); assertEquals(new Dimension(400,600), dimension); } }
6. The Parts (part3) // name should be descriptive // i.e. test<MethodName>[Should] // i.e. test[Given][When][Then] public void testGetMimeType() { MimeType mimeType = jpegImage.getMimeType(); // [descriptive message,] <expected>, <actual> assertEquals(MimeType.JPEG, mimeType); }
10. Do not catch exceptions unless that's what you're testing
11. The Goal Name File functional unit unit integration Controller Service Repository DB My Vaction Pic C:ics01.jpeg Upload should store in repo should persist in db should persist inputs should serve requests
18. Atomic Examples public Set select(Matcher matcher) { Set matchingObjects = new HashSet(); for(Object candidate : this.source) { if(matcher.matches(candidate)) { matchingObjects.add(candidate); } } return matchingObjects; } public Person get(Long id) { Assert.notNull(id, “Should have id to retrieve a Person.”); return repo.get(id); }
19. Test Organization public class PersonManager { public void create(Person person) {…} public Person retrieve(Long id) {…} public void update(Person person) {…} public void delete(Long id) {...} }
20. Test Organization :: SUT public class Container { public Long add(Object newObject) {…} public Object get(Long id) {…} public void replace(Long id, Object replacement) {…} public void delete(Long id) {...} }
21. Test Organization :: Simple Test Per Class public class ContainerTest extends TestCase { public void testAdd() {...} public void testGet() {...} public void testReplace() {...} public void testDelete() {...} }
22. Test Organization :: Non-Simple Test Per Class public class ContainerTest extends TestCase { public void testGivenNonFull_WhenAdd_ThenPutInContainer() {…} public void testGivenFull_WhenAdd_ThenThrowException() {...} public void testGivenKnownId_WhenGet_ThenReturnObject() {…} public void testGivenUnknownId_WhenGet_ThenNull() {...} public void testGivenKnownId_WhenReplace_ThenRemoveAndAdd() {…} public void testGivenUnknownId_WhenReplace_ThenAdd() {...} public void testGivenKnownId_WhenDelete_ThenRemove() {…} public void testGivenUnknownId_WhenDelete_ThenThrowException() {...} }
23. Test Organization :: Test Per Fixture public class ContainerTest extends TestSuite { public static class GivenNonFull extends TestCase { public void testWhenAdd_ThenPutInContainer() {...} } public static class GivenFull extends TestCase { public void testWhenAdd_ThenThrowException() {...} } public static class GivenKnownId extends TestCase { public void testWhenGet_ThenReturnObject() {…} public void testWhenReplace_ThenRemoveAndAdd() {…} public void testWhenDelete_ThenRemove() {…} } public static class GivenUnknownId extends TestCase { public void testWhenGet_ThenNull() {...} public void testWhenReplace_ThenAdd() {...} public void testWhenDelete_ThenThrowException() {...} } }
24. Test Organization :: Test Per Feature public class ContainerTest extends TestCase { public static class WhenAdd extends TestCase { public void testGivenNonFull_ThenPutInContainer() {…} public void testGivenFull_ThenThrowException() {...} } public static class WhenGet extends TestCase { public void testGivenKnownId_ThenReturnObject() {…} public void testGivenUnknownId_ThenNull() {...} } public static class WhenReplace extends TestCase { public void testGivenKnownId_ThenRemoveAndAdd() {…} public void testGivenUnknownId_ThenAdd() {...} } public static class WhenDelete extends TestCase { public void testGivenKnownId_ThenRemove() {…} public void testGivenUnknownId_ThenThrowException() {...} } }
25.
26.
27. Tip :: Fixtures (part2) public static class WhenTake extends TestCase { public void testGivenFoodCouponWithinPromoDates_ThenRetrieveFoodGift() { Coupon givenCoupon = new CouponBuilder() .for(CouponType.Food) .within(promoDate.start,promoDate.end) .build(); Gift gift = storeOutlet.take(givenCoupon); assertEquals(Gift.Food, gift); } public void testGivenFoodCouponOutsidePromoDates_ThenRetrieveNull() { Coupon givenCoupon = new CouponBuilder() .for(CouponType.Food) .outside(promoDate.start,promoDate.end) .build(); Gift gift = storeOutlet.take(givenCoupon); assertNull(gift); } }
33. Data Flow :: Code public Entity createEntity(Long id, String name) { return new Entity(id, name); }
34. Data Flow :: Diagram createEntity id name Entity processing
35. Data Flow :: Code public Entity createEntity(Long id, String name) { Entity entity = registry.contains(id) ? registry.get(id) : new Entity(id); entity.setName(name); registry.store(entity); return entity; }
36. Data Flow createEntity id name Entity processing contains(..) get(..) store(..) registry
37. Data Flow createEntity id name Entity processing contains(..) get(..) store(..) Direct Input Indirect Input Direct Output Indirect Output Collaborator registry
38. Test Doubles :: Dummies (part1) createEntity id name Entity processing contains(..) get(..) store(..) registry
39. Test Doubles :: Dummies (part2) private SystemUnderTest sut; private Registry registry; protected void setUp() { super.setUp(); registry = new RegistryImpl(); sut.setRegistry(registry); } … public void testCreateEntity() { Long givenDummyId = -1L; String givenDummyName = “dummy name”; Entity entity = createEntity(givenDummyId, givenDummyName); assertEquals(“Should have set name of entity.”, givenDummyName, entity.getName()); }
40. Test Doubles :: Mocks (part1) createEntity id name Entity processing contains(..) get(..) store(..) registry