Skip to content

Commit 52bc347

Browse files
committed
added object mother with fluent builder example
1 parent a9c9fab commit 52bc347

File tree

9 files changed

+184
-0
lines changed

9 files changed

+184
-0
lines changed

patterns/build.gradle

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
buildscript {
2+
repositories {
3+
jcenter()
4+
}
5+
}
6+
7+
apply plugin: 'java'
8+
9+
version = '0.0.1-SNAPSHOT'
10+
sourceCompatibility = 1.8
11+
12+
repositories {
13+
mavenLocal()
14+
mavenCentral()
15+
}
16+
17+
dependencies {
18+
compileOnly 'org.projectlombok:lombok:1.18.2'
19+
testCompile 'org.junit.jupiter:junit-jupiter-engine:5.0.1'
20+
testCompile 'org.assertj:assertj-core:2.6.0'
21+
}
22+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.reflectoring.objectmother;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
6+
@Data
7+
@Builder
8+
class Address {
9+
10+
private String street;
11+
12+
private String houseNumber;
13+
14+
private String zipCode;
15+
16+
private String city;
17+
18+
private String country;
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.reflectoring.objectmother;
2+
3+
import java.util.List;
4+
5+
import lombok.Builder;
6+
import lombok.Data;
7+
8+
@Data
9+
@Builder
10+
class Invoice {
11+
12+
private long id;
13+
14+
private Address address;
15+
16+
private List<InvoiceItem> items;
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.reflectoring.objectmother;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
6+
@Data
7+
@Builder
8+
class InvoiceItem {
9+
10+
private long amount;
11+
12+
private long price;
13+
14+
private String productName;
15+
16+
private double taxFactor;
17+
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.reflectoring.objectmother;
2+
3+
class AddressMother {
4+
5+
static Address.AddressBuilder complete() {
6+
return Address.builder()
7+
.street("Hollywood Boulevard")
8+
.houseNumber("4711")
9+
.zipCode("90210")
10+
.country("US")
11+
.city("Los Angeles");
12+
}
13+
14+
static Address.AddressBuilder abroad() {
15+
return complete()
16+
.country("DE");
17+
}
18+
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.reflectoring.objectmother;
2+
3+
class InvoiceItemMother {
4+
5+
static InvoiceItem.InvoiceItemBuilder complete() {
6+
return InvoiceItem.builder()
7+
.amount(1)
8+
.price(1234L)
9+
.productName("The Hitchhiker's Guide to the Galaxy")
10+
.taxFactor(0.19d);
11+
}
12+
13+
static InvoiceItem.InvoiceItemBuilder withNegativePrice() {
14+
return InvoiceItem.builder()
15+
.amount(1)
16+
.price(-1234L)
17+
.productName("The Hitchhiker's Guide to the Galaxy")
18+
.taxFactor(0.19d);
19+
}
20+
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.reflectoring.objectmother;
2+
3+
import java.util.Collections;
4+
5+
class InvoiceMother {
6+
7+
static Invoice.InvoiceBuilder complete() {
8+
return Invoice.builder()
9+
.id(42L)
10+
.address(AddressMother.complete()
11+
.build())
12+
.items(Collections.singletonList(
13+
InvoiceItemMother.complete()
14+
.build()));
15+
}
16+
17+
static Invoice.InvoiceBuilder refund() {
18+
return Invoice.builder()
19+
.id(42L)
20+
.address(AddressMother.complete()
21+
.build())
22+
.items(Collections.singletonList(
23+
InvoiceItemMother.withNegativePrice()
24+
.build()));
25+
}
26+
27+
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.reflectoring.objectmother;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.assertj.core.api.Assertions.*;
5+
6+
class ObjectMotherClient {
7+
8+
@Test
9+
void invoiceWithAbroadAddress() {
10+
Invoice invoice = InvoiceMother.complete()
11+
.address(AddressMother.abroad()
12+
.build())
13+
.build();
14+
assertThat(invoice.getAddress().getCountry()).isEqualTo("DE");
15+
}
16+
17+
@Test
18+
void invoiceWithMissingHouseNumber() {
19+
Invoice invoice = InvoiceMother.complete()
20+
.address(AddressMother.complete()
21+
.houseNumber(null)
22+
.build())
23+
.build();
24+
assertThat(invoice.getAddress().getHouseNumber()).isNull();
25+
}
26+
27+
@Test
28+
void invoiceWithNegativeTotal() {
29+
Invoice invoice = InvoiceMother.refund()
30+
.build();
31+
double sum = invoice.getItems().stream()
32+
.mapToDouble(item -> item.getAmount() * item.getPrice())
33+
.sum();
34+
assertThat(sum).isNegative();
35+
}
36+
37+
}

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ include 'logging'
2323

2424
include 'junit:conditions'
2525

26+
include 'patterns'
27+
2628

2729

0 commit comments

Comments
 (0)