added object mother with fluent builder example
This commit is contained in:
22
patterns/build.gradle
Normal file
22
patterns/build.gradle
Normal file
@@ -0,0 +1,22 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'java'
|
||||
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly 'org.projectlombok:lombok:1.18.2'
|
||||
testCompile 'org.junit.jupiter:junit-jupiter-engine:5.0.1'
|
||||
testCompile 'org.assertj:assertj-core:2.6.0'
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package io.reflectoring.objectmother;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
class Address {
|
||||
|
||||
private String street;
|
||||
|
||||
private String houseNumber;
|
||||
|
||||
private String zipCode;
|
||||
|
||||
private String city;
|
||||
|
||||
private String country;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.reflectoring.objectmother;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
class Invoice {
|
||||
|
||||
private long id;
|
||||
|
||||
private Address address;
|
||||
|
||||
private List<InvoiceItem> items;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.reflectoring.objectmother;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
class InvoiceItem {
|
||||
|
||||
private long amount;
|
||||
|
||||
private long price;
|
||||
|
||||
private String productName;
|
||||
|
||||
private double taxFactor;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package io.reflectoring.objectmother;
|
||||
|
||||
class AddressMother {
|
||||
|
||||
static Address.AddressBuilder complete() {
|
||||
return Address.builder()
|
||||
.street("Hollywood Boulevard")
|
||||
.houseNumber("4711")
|
||||
.zipCode("90210")
|
||||
.country("US")
|
||||
.city("Los Angeles");
|
||||
}
|
||||
|
||||
static Address.AddressBuilder abroad() {
|
||||
return complete()
|
||||
.country("DE");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package io.reflectoring.objectmother;
|
||||
|
||||
class InvoiceItemMother {
|
||||
|
||||
static InvoiceItem.InvoiceItemBuilder complete() {
|
||||
return InvoiceItem.builder()
|
||||
.amount(1)
|
||||
.price(1234L)
|
||||
.productName("The Hitchhiker's Guide to the Galaxy")
|
||||
.taxFactor(0.19d);
|
||||
}
|
||||
|
||||
static InvoiceItem.InvoiceItemBuilder withNegativePrice() {
|
||||
return InvoiceItem.builder()
|
||||
.amount(1)
|
||||
.price(-1234L)
|
||||
.productName("The Hitchhiker's Guide to the Galaxy")
|
||||
.taxFactor(0.19d);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package io.reflectoring.objectmother;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
class InvoiceMother {
|
||||
|
||||
static Invoice.InvoiceBuilder complete() {
|
||||
return Invoice.builder()
|
||||
.id(42L)
|
||||
.address(AddressMother.complete()
|
||||
.build())
|
||||
.items(Collections.singletonList(
|
||||
InvoiceItemMother.complete()
|
||||
.build()));
|
||||
}
|
||||
|
||||
static Invoice.InvoiceBuilder refund() {
|
||||
return Invoice.builder()
|
||||
.id(42L)
|
||||
.address(AddressMother.complete()
|
||||
.build())
|
||||
.items(Collections.singletonList(
|
||||
InvoiceItemMother.withNegativePrice()
|
||||
.build()));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package io.reflectoring.objectmother;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
class ObjectMotherClient {
|
||||
|
||||
@Test
|
||||
void invoiceWithAbroadAddress() {
|
||||
Invoice invoice = InvoiceMother.complete()
|
||||
.address(AddressMother.abroad()
|
||||
.build())
|
||||
.build();
|
||||
assertThat(invoice.getAddress().getCountry()).isEqualTo("DE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void invoiceWithMissingHouseNumber() {
|
||||
Invoice invoice = InvoiceMother.complete()
|
||||
.address(AddressMother.complete()
|
||||
.houseNumber(null)
|
||||
.build())
|
||||
.build();
|
||||
assertThat(invoice.getAddress().getHouseNumber()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void invoiceWithNegativeTotal() {
|
||||
Invoice invoice = InvoiceMother.refund()
|
||||
.build();
|
||||
double sum = invoice.getItems().stream()
|
||||
.mapToDouble(item -> item.getAmount() * item.getPrice())
|
||||
.sum();
|
||||
assertThat(sum).isNegative();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,5 +23,7 @@ include 'logging'
|
||||
|
||||
include 'junit:conditions'
|
||||
|
||||
include 'patterns'
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user