Struggling to Create Maintainable Unit Tests?

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Struggling to Create Maintainable Unit Tests? - Presentation Transcript

    1.  
    2.  
    3. Protocol => Mocks Data => Builders
    4. Roadmap
      • Define Maintainable Unit Test
      • Create a Shared Vocabulary
      • Examples
      • When to Use Mocks
    5. Define Maintainable Unit Test
    6. 10% of Unit Tests Break
      • 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.
      • — Joel Spolsky
    7. Maintainable Unit Tests
      • The benefits of unit tests outweigh the costs as a codebase evolves.
    8. Shared Vocabulary
    9. Mocks Builders
    10.  
    11. xUnit Test Patterns
    12. Mocks Builders
    13. Mock Object
      • A mock object is a simulated object that mimics the behaviour of a real object in controlled ways.
    14. Test Data Builder
      • Builder Pattern
      • build() method
      • Default values
      • Chainable configuration methods
    15. Shared Vocabulary
      • 4–Phase Test
      • System Under Test
      • Depended–On Component
      • Mock Objects
      • Test Data Builders
    16. Examples
    17.  
    18. Domain Model
    19. public void should_recognize_pediatric_patient() { // setup Patient sut = aPatient ().age(17).build(); // exercise and verify assertTrue (sut.isPediatric()); assertTrue (!sut.isAdult()); }
    20. public void should_recognize_pediatric_patient() { // setup Patient sut = aPatient ().age(17).build(); // exercise and verify assertTrue (sut.isPediatric()); assertTrue (!sut.isAdult()); }
    21. public void should_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
      • Patient must be registered at the same Facility as the Care Team.
      • Patient must meet the Care Team Membership Criteria (appropriate Age Category and Diagnosis).
    23. public class CareTeamMembershipService implements ICareTeamMembershipService { private CareTeamMembershipDao careTeamMembershipDao ; public void addMember(Patient patient, CareTeam careTeam) { if (!patient.isRegisteredAt(careTeam.getFacility())) { throw new CareTeamAdminException(); } if (!patient.meets(careTeam.getMembershipCriteria())) { throw new CareTeamAdminException(); } careTeamMembershipDao .create( patient.getId(), careTeam.getId()); } }
    24. private final CareTeamMembershipDao careTeamMembershipDao = context .mock(CareTeamMembershipDao. class ); private final ICareTeamMembershipService sut = createCareTeamMembershipService(); public void should_permit_add_for_appropriate_care_team() { // setup final Facility jacobi = aFacility ().build(); final Patient patient = aPatient ().at(jacobi).age(18) .with(Diagnosis. DIABETES ).build(); final CareTeam careTeam = anAdultCareTeam ().at(jacobi) .with(Diagnosis. DIABETES ).build(); // verify context .checking( new Expectations() {{ one( careTeamMembershipDao ).create( patient.getId(), careTeam.getId()); }}); // exercise sut .addMember(patient, careTeam); }
    25. private final CareTeamMembershipDao careTeamMembershipDao = context .mock(CareTeamMembershipDao. class ); private final ICareTeamMembershipService sut = createCareTeamMembershipService(); // verify @Test (expected=CareTeamAdminException. class ) public void should_disallow_add_adult_to_pediatric_care_team() { // setup final Facility jacobi = aFacility ().build(); final Patient patient = aPatient ().at(jacobi).age(18) . with (Diagnosis. DIABETES ).build(); final CareTeam careTeam = aPediatricCareTeam ().at(jacobi) .with(Diagnosis. DIABETES ).build(); // exercise sut .addMember(patient, careTeam); }
    26. Examples
      • Test vs Production API
      • Builders as shared test asset
      • Mocks and Builders as Firewalls
    27. When to Use Mocks
    28. Mocks Builders
    29. Protocol Data
    30. Protocol => Mocks Data => Builders
    31. Now What?
      • Read xUnit Test Patterns
      • Read Nat Pryce’s Builder posts
      • Distinguish Protocol vs Data
      • Try using Mocks with Builders
    32.  
    33.  
    34. Contact
      • Jason Cheong-Kee-You
      • [email_address]
      • Alistair McKinnell
      • [email_address]
    35. Resources
      • xUnit Test Patterns
      • http://xunitpatterns.com/
      • jMock
      • http://www.jmock.org/
      • Test Data Builders
      • http://nat.truemesh.com/archives/000714.html
    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.  

    + Alistair McKinnellAlistair McKinnell, 8 months ago

    custom

    413 views, 1 favs, 2 embeds more stats

    Learn how to create maintainable unit tests by usin more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 413
      • 382 on SlideShare
      • 31 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 13
    Most viewed embeds
    • 30 views on http://www.valuablecode.com
    • 1 views on http://74.125.47.132

    more

    All embeds
    • 30 views on http://www.valuablecode.com
    • 1 views on http://74.125.47.132

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories