Struggling to Create Maintainable Unit Tests?

Download Struggling to Create Maintainable Unit Tests?

Post on 18-Nov-2014

4.403 views

Category:

Technology

0 download

Embed Size (px)

DESCRIPTION

Learn how to create maintainable unit tests by using jMock with Test Data Builders.

TRANSCRIPT

<ul><li> 1. </li></ul> <p> 2. 3. Protocol =&gt; Mocks Data =&gt; Builders 4. Roadmap </p> <ul><li>Define Maintainable Unit Test </li></ul> <ul><li>Create a Shared Vocabulary </li></ul> <ul><li>Examples </li></ul> <ul><li>When to Use Mocks </li></ul> <p> 5. Define Maintainable Unit Test 6. 10% of Unit Tests Break</p> <ul><li>If you really have a lot of unit tests, the amount of investment you'll have to make in maintaining those unit tests, keeping them up-to-date and keeping them passing, starts to become disproportional to the amount of benefit that you get out of them. </li></ul> <ul><li>Joel Spolsky </li></ul> <p> 7. Maintainable Unit Tests </p> <ul><li>The benefits of unit tests outweigh the costs as a codebase evolves.</li></ul> <p> 8. Shared Vocabulary 9. Mocks Builders 10. 11. xUnit Test Patterns 12. Mocks Builders 13. Mock Object </p> <ul><li>Amock objectis a simulated object that mimics the behaviour of a real object in controlled ways. </li></ul> <p> 14. Test Data Builder </p> <ul><li>Builder Pattern </li></ul> <ul><li>build()method </li></ul> <ul><li>Default values </li></ul> <ul><li>Chainable configuration methods </li></ul> <p> 15. Shared Vocabulary </p> <ul><li>4Phase Test</li></ul> <ul><li>System Under Test </li></ul> <ul><li>DependedOn Component </li></ul> <ul><li>Mock Objects </li></ul> <ul><li>Test Data Builders </li></ul> <p> 16. Examples 17. 18. Domain Model 19. public voidshould_recognize_pediatric_patient() { // setup Patient sut =aPatient ().age(17).build(); // exercise and verify assertTrue (sut.isPediatric()); assertTrue (!sut.isAdult()); } 20. public voidshould_recognize_pediatric_patient() { // setup Patient sut =aPatient ().age(17).build(); // exercise and verify assertTrue (sut.isPediatric()); assertTrue (!sut.isAdult()); } 21. public voidshould_recognize_pediatric_patient() { // setup Patient sut =aPatient ().age(17).build(); // exercise and verify assertTrue (sut.isPediatric()); assertTrue (!sut.isAdult()); } aPatient ().dob(april(1, 1992)).build(); 22. Add Patient to Care Team </p> <ul><li>Patient must be registered at the same Facility as the Care Team. </li></ul> <ul><li>Patient must meet the Care Team Membership Criteria (appropriate Age Category and Diagnosis). </li></ul> <p> 23. public classCareTeamMembershipService implementsICareTeamMembershipService { privateCareTeamMembershipDaocareTeamMembershipDao ; public voidaddMember(Patient patient, CareTeam careTeam) { if(!patient.isRegisteredAt(careTeam.getFacility())) { throw newCareTeamAdminException(); } if(!patient.meets(careTeam.getMembershipCriteria())) { throw newCareTeamAdminException(); } careTeamMembershipDao .create( patient.getId(), careTeam.getId());} } 24. private finalCareTeamMembershipDaocareTeamMembershipDao= context .mock(CareTeamMembershipDao. class ); private finalICareTeamMembershipServicesut= createCareTeamMembershipService(); public voidshould_permit_add_for_appropriate_care_team() { // setup finalFacility jacobi =aFacility ().build(); finalPatient patient =aPatient ().at(jacobi).age(18) .with(Diagnosis. DIABETES ).build(); finalCareTeam careTeam =anAdultCareTeam ().at(jacobi) .with(Diagnosis. DIABETES ).build(); // verify context .checking( newExpectations() {{ one( careTeamMembershipDao ).create( patient.getId(), careTeam.getId()); }}); // exercise sut .addMember(patient, careTeam); } 25. private finalCareTeamMembershipDaocareTeamMembershipDao= context .mock(CareTeamMembershipDao. class ); private finalICareTeamMembershipServicesut= createCareTeamMembershipService(); // verify @Test (expected=CareTeamAdminException. class ) public voidshould_disallow_add_adult_to_pediatric_care_team() { // setup finalFacility jacobi =aFacility ().build(); finalPatient patient =aPatient ().at(jacobi).age(18) . with (Diagnosis. DIABETES ).build(); finalCareTeam careTeam =aPediatricCareTeam ().at(jacobi) .with(Diagnosis. DIABETES ).build(); // exercise sut .addMember(patient, careTeam); } 26. Examples </p> <ul><li>Test vs Production API</li></ul> <ul><li>Builders as shared test asset </li></ul> <ul><li>Mocks and Builders as Firewalls </li></ul> <p> 27. When to UseMocks 28. Mocks Builders 29. Protocol Data 30. Protocol =&gt; Mocks Data =&gt; Builders 31. Now What? </p> <ul><li>ReadxUnit Test Patterns </li></ul> <ul><li>Read Nat Pryces Builder posts </li></ul> <ul><li>Distinguish Protocol vs Data </li></ul> <ul><li>Try using Mocks with Builders </li></ul> <p> 32. 33. 34. Contact </p> <ul><li>Jason Cheong-Kee-You </li></ul> <ul><li>[email_address] </li></ul> <ul><li>Alistair McKinnell </li></ul> <ul><li>[email_address] </li></ul> <p> 35. Resources </p> <ul><li>xUnit Test Patterns </li></ul> <ul><li>http://xunitpatterns.com/ </li></ul> <ul><li>jMock </li></ul> <ul><li>http://www.jmock.org/ </li></ul> <ul><li>Test Data Builders </li></ul> <ul><li>http://nat.truemesh.com/archives/000714.html </li></ul> <p> 36. Credits Obstacle Race http://www.flickr.com/photos/foxypar4/1004464889/ Einstein Blackboard Writing Generator http://generator.kitt.net/2006/12/ einstein-blackboard-writing-generator.html Holy Grail http://www.flickr.com/photos/ spiritual_marketplace/2207966935/ 37. </p>