File tree Expand file tree Collapse file tree
main/java/io/reflectoring/objectmother
test/java/io/reflectoring/objectmother Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -23,5 +23,7 @@ include 'logging'
2323
2424include ' junit:conditions'
2525
26+ include ' patterns'
27+
2628
2729
You can’t perform that action at this time.
0 commit comments