Appendix B. Source Code

Test Doubles

LISTING B.1 PremiumPurchaseMatcher: A custom matcher that matches specific business rules.

import org.hamcrest.Description;import org.hamcrest.TypeSafeMatcher;public class PremiumPurchaseMatcher extends TypeSafeMatcher<Purchase> {    @Override    public boolean matchesSafely(Purchase purchase) {        return purchase.getPrice() > 1000 && purchase.getItemCount() < 5;    }    @Override    public void describeTo(Description desc) {        desc.appendText("A purchase with the " +                "total price > 1000 and fewer than 5 items");    }}

Data-driven and Combinatorial Testing

LISTING B.2 A JUnit-based implementation of a parameterized test.

Get Developer Testing: Building Quality into Software now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.