Skip to content

Instantly share code, notes, and snippets.

package org.craftedsw.testingbuilders;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
java.lang.AssertionError:
Expected: property "firstName" = "Another dude"
but: property "firstName" was "Sandro"
java.lang.AssertionError:
Expected: property "firstName" = "Another dude" , property "age" = a value greater than <60>
but: property "firstName" was "Sandro" , property "age" <25> was less than <60>
Person person = new Person();
person.setFirstName("Sandro");
person.setAge(35);
Country uk = new Country();
uk.setName("United Kingdom");
Address address = new Address();
address.setPostcode("1234556");
address.setCity("London");
// Static imports
import static org.craftedsw.beanpropertymatcher.matcher.BeanMatcher.has;
import static org.craftedsw.beanpropertymatcher.matcher.BeanPropertyMatcher.property;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
// Imagine that you have a method that returns an object Person
public class PricingService {
private PriceCalculation priceCalculation;
private VoucherDiscountCalculation voucherDiscountCalculation;
private PrimeUserDiscountCalculation primeUserDiscountCalculation;
public PricingService(PriceCalculation priceCalculation, VoucherDiscountCalculation voucherDiscountCalculation,
PrimeUserDiscountCalculation primeUserDiscountCalculation) {
this.priceCalculation = priceCalculation;
this.voucherDiscountCalculation = voucherDiscountCalculation;
public abstract class PricingService {
private PriceCalculation priceCalculation;
private VoucherDiscountCalculation voucherDiscountCalculation;
public double calculatePrice(ShoppingBasket shoppingBasket, User user, String voucher) {
double discount = calculateDiscount(user);
double total = 0;
for (ShoppingBasket.Item item : shoppingBasket.items()) {
total += priceCalculation.calculateProductPrice(item.getProduct(), item.getQuantity());
@RunWith(MockitoJUnitRunner.class)
public class PricingServiceTest {
private static final String NO_VOUCHER = "";
private static final String FIVE_POUNDS_VOUCHER = "5";
private TestablePricingService pricingService = new TestablePricingService();
private ShoppingBasket shoppingBasket;
@Mock private PriceCalculation priceCalculation;
public class VoucherPricingService extends UserDiscountPricingService {
private VoucherService voucherService;
@Override
protected double applyAdditionalDiscounts(double total, String voucher) {
VoucherDiscountCalculation voucherDiscountCalculation = new VoucherDiscountCalculation(voucherService);
return voucherDiscountCalculation.calculateVoucherDiscount(total, voucher);
}
public class VoucherDiscountCalculation {
private VoucherService voucherService;
public VoucherDiscountCalculation(VoucherService voucherService) {
this.voucherService = voucherService;
}
public double calculateVoucherDiscount(double total, String voucher) {
double voucherValue = voucherService.getVoucherValue(voucher);
double totalAfterValue = total - voucherValue;