Top Banner
37

Struggling to Create Maintainable Unit Tests?

Nov 18, 2014

Download

Technology

Learn how to create maintainable unit tests by using jMock with Test Data Builders.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Struggling to Create Maintainable Unit Tests?
Page 2: Struggling to Create Maintainable Unit Tests?
Page 3: Struggling to Create Maintainable Unit Tests?

Protocol => Mocks

Data => Builders

Page 4: Struggling to Create Maintainable Unit Tests?

Roadmap

• Define Maintainable Unit Test

• Create a Shared Vocabulary

• Examples

• When to Use Mocks

Page 5: Struggling to Create Maintainable Unit Tests?

DefineMaintainable

Unit Test

Page 6: Struggling to Create Maintainable Unit Tests?

10% of Unit Tests Break

If you really have a lot of unit tests, the amount of investment you'll have to makein 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

Page 7: Struggling to Create Maintainable Unit Tests?

Maintainable Unit Tests

The benefits of unit tests outweigh the costs as a codebase evolves.

Page 8: Struggling to Create Maintainable Unit Tests?

SharedVocabulary

Page 9: Struggling to Create Maintainable Unit Tests?

Mocks

Builders

Page 10: Struggling to Create Maintainable Unit Tests?
Page 11: Struggling to Create Maintainable Unit Tests?

xUnit Test Patterns

Page 12: Struggling to Create Maintainable Unit Tests?

Mocks

Builders

Page 13: Struggling to Create Maintainable Unit Tests?

Mock Object

A mock object is a simulated object that mimics the behaviour of a real object in controlled ways.

Page 14: Struggling to Create Maintainable Unit Tests?

Test Data Builder

• Builder Pattern

• build() method

• Default values

• Chainable configuration methods

Page 15: Struggling to Create Maintainable Unit Tests?

Shared Vocabulary

• 4–Phase Test

• System Under Test

• Depended–On Component

• Mock Objects

• Test Data Builders

Page 16: Struggling to Create Maintainable Unit Tests?

Examples

Page 17: Struggling to Create Maintainable Unit Tests?
Page 18: Struggling to Create Maintainable Unit Tests?

Domain Model

Page 19: Struggling to Create Maintainable Unit Tests?

public void should_recognize_pediatric_patient(){ // setup Patient sut = aPatient().age(17).build();

// exercise and verify assertTrue(sut.isPediatric()); assertTrue(!sut.isAdult());}

Page 20: Struggling to Create Maintainable Unit Tests?

public void should_recognize_pediatric_patient(){ // setup Patient sut = aPatient().age(17).build();

// exercise and verify assertTrue(sut.isPediatric()); assertTrue(!sut.isAdult());}

Page 21: Struggling to Create Maintainable Unit Tests?

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();

Page 22: Struggling to Create Maintainable Unit Tests?

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).

Page 23: Struggling to Create Maintainable Unit Tests?

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()); }

}

Page 24: Struggling to Create Maintainable Unit Tests?

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);}

Page 25: Struggling to Create Maintainable Unit Tests?

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);}

Page 26: Struggling to Create Maintainable Unit Tests?

Examples

• Test vs Production API

• Builders as shared test asset

• Mocks and Builders as Firewalls

Page 27: Struggling to Create Maintainable Unit Tests?

When to Use Mocks

Page 28: Struggling to Create Maintainable Unit Tests?

Mocks

Builders

Page 29: Struggling to Create Maintainable Unit Tests?

Protocol

Data

Page 30: Struggling to Create Maintainable Unit Tests?

Protocol => Mocks

Data => Builders

Page 31: Struggling to Create Maintainable Unit Tests?

Now What?

• Read xUnit Test Patterns

• Read Nat Pryce’s Builder posts

• Distinguish Protocol vs Data

• Try using Mocks with Builders

Page 32: Struggling to Create Maintainable Unit Tests?
Page 33: Struggling to Create Maintainable Unit Tests?
Page 35: Struggling to Create Maintainable Unit Tests?

Resources

xUnit Test Patternshttp://xunitpatterns.com/

jMockhttp://www.jmock.org/

Test Data Buildershttp://nat.truemesh.com/archives/000714.html

Page 36: Struggling to Create Maintainable Unit Tests?

Credits

Obstacle Racehttp://www.flickr.com/photos/foxypar4/1004464889/

Einstein Blackboard Writing Generatorhttp://generator.kitt.net/2006/12/einstein-blackboard-writing-generator.html

Holy Grailhttp://www.flickr.com/photos/spiritual_marketplace/2207966935/

Page 37: Struggling to Create Maintainable Unit Tests?