This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.lang.AssertionError: | |
Expected: property "firstName" = "Another dude" | |
but: property "firstName" was "Sandro" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |