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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.