Protocol => Mocks Data => Builders
Roadmap <ul><li>Define Maintainable Unit Test </li></ul><ul><li>Create a Shared Vocabulary </li></ul><ul><li>Examples </li...
Define Maintainable Unit Test
10% of Unit Tests Break  <ul><li>If you really have a lot of unit tests, the amount of investment you'll have to make in m...
Maintainable Unit Tests <ul><li>The benefits of unit tests outweigh the costs as a codebase evolves.  </li></ul>
Shared Vocabulary
Mocks Builders
 
xUnit Test Patterns
Mocks Builders
Mock Object <ul><li>A  mock object  is a simulated object that mimics the behaviour of a real object in controlled ways. <...
Test Data Builder <ul><li>Builder Pattern </li></ul><ul><li>build()  method </li></ul><ul><li>Default values </li></ul><ul...
Shared Vocabulary <ul><li>4–Phase Test  </li></ul><ul><li>System Under Test </li></ul><ul><li>Depended–On Component </li><...
Examples
 
Domain Model
public   void  should_recognize_pediatric_patient() { // setup Patient sut =  aPatient ().age(17).build(); // exercise and...
public   void  should_recognize_pediatric_patient() { // setup Patient sut =  aPatient ().age(17).build(); // exercise and...
public   void  should_recognize_pediatric_patient() { // setup Patient sut =  aPatient ().age(17).build(); // exercise and...
Add Patient to Care Team <ul><li>Patient must be registered at the same Facility as the Care Team. </li></ul><ul><li>Patie...
public   class  CareTeamMembershipService   implements  ICareTeamMembershipService { private  CareTeamMembershipDao  careT...
private   final  CareTeamMembershipDao  careTeamMembershipDao  =   context .mock(CareTeamMembershipDao. class ); private  ...
private   final  CareTeamMembershipDao  careTeamMembershipDao  =   context .mock(CareTeamMembershipDao. class ); private  ...
Examples <ul><li>Test vs Production API  </li></ul><ul><li>Builders as shared test asset </li></ul><ul><li>Mocks and Build...
When to Use  Mocks
Mocks Builders
Protocol Data
Protocol => Mocks Data => Builders
Now What? <ul><li>Read  xUnit Test Patterns </li></ul><ul><li>Read Nat Pryce’s Builder posts </li></ul><ul><li>Distinguish...
 
 
Contact <ul><li>Jason Cheong-Kee-You </li></ul><ul><li>[email_address] </li></ul><ul><li>Alistair McKinnell </li></ul><ul>...
Resources <ul><li>xUnit Test Patterns </li></ul><ul><li>http://xunitpatterns.com/ </li></ul><ul><li>jMock </li></ul><ul><l...
Credits Obstacle Race http://www.flickr.com/photos/foxypar4/1004464889/ Einstein Blackboard Writing Generator http://gener...
 
Upcoming SlideShare
Loading in...5
×

Struggling to Create Maintainable Unit Tests?

3,702

Published on

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

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,702
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
40
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Struggling to Create Maintainable Unit Tests?

  1. 3. Protocol => Mocks Data => Builders
  2. 4. Roadmap <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>
  3. 5. Define Maintainable Unit Test
  4. 6. 10% of Unit Tests Break <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>
  5. 7. Maintainable Unit Tests <ul><li>The benefits of unit tests outweigh the costs as a codebase evolves. </li></ul>
  6. 8. Shared Vocabulary
  7. 9. Mocks Builders
  8. 11. xUnit Test Patterns
  9. 12. Mocks Builders
  10. 13. Mock Object <ul><li>A mock object is a simulated object that mimics the behaviour of a real object in controlled ways. </li></ul>
  11. 14. Test Data Builder <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>
  12. 15. Shared Vocabulary <ul><li>4–Phase Test </li></ul><ul><li>System Under Test </li></ul><ul><li>Depended–On Component </li></ul><ul><li>Mock Objects </li></ul><ul><li>Test Data Builders </li></ul>
  13. 16. Examples
  14. 18. Domain Model
  15. 19. public void should_recognize_pediatric_patient() { // setup Patient sut = aPatient ().age(17).build(); // exercise and verify assertTrue (sut.isPediatric()); assertTrue (!sut.isAdult()); }
  16. 20. public void should_recognize_pediatric_patient() { // setup Patient sut = aPatient ().age(17).build(); // exercise and verify assertTrue (sut.isPediatric()); assertTrue (!sut.isAdult()); }
  17. 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();
  18. 22. Add Patient to Care Team <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>
  19. 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()); } }
  20. 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); }
  21. 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); }
  22. 26. Examples <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>
  23. 27. When to Use Mocks
  24. 28. Mocks Builders
  25. 29. Protocol Data
  26. 30. Protocol => Mocks Data => Builders
  27. 31. Now What? <ul><li>Read xUnit Test Patterns </li></ul><ul><li>Read Nat Pryce’s Builder posts </li></ul><ul><li>Distinguish Protocol vs Data </li></ul><ul><li>Try using Mocks with Builders </li></ul>
  28. 34. Contact <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>
  29. 35. Resources <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>
  30. 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/
  1. Gostou de algum slide específico?

    Recortar slides é uma maneira fácil de colecionar informações para acessar mais tarde.

×