Last active
August 12, 2020 10:23
-
-
Save bverbeken/5724148 to your computer and use it in GitHub Desktop.
Builder pattern for test fixtures
This file contains hidden or 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 AddressBuilder { | |
public static AddressBuilder anAddress(){ | |
return new AddressBuilder(); | |
} | |
@Override | |
public Address build() { | |
Address address = new Address(); | |
// ... set fields, you get the idea | |
return address; | |
} | |
} |
This file contains hidden or 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 Person build(){ | |
Person person = new Person(); | |
person.addAddresses(this.addresses); | |
return person; | |
} |
This file contains hidden or 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
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class PersonBuilder { | |
private String firstName = "Ben"; | |
private String lastName = "Verbeken"; | |
private List<Address> addresses = new ArrayList<Address>(); | |
public static PersonBuilder aPerson() { | |
return new PersonBuilder(); | |
} | |
@Override | |
public Person build() { | |
Person person = new Person(); | |
person.setFirstName(this.firstName); | |
person.setLastName(this.lastName); | |
person.setAddresses(this.addresses); | |
return person; | |
} | |
public PersonBuilder withFirstName(String firstName) { | |
this.firstName = firstName; | |
return this; | |
} | |
public PersonBuilder withLastName(String lastName) { | |
this.lastName = lastName; | |
return this; | |
} | |
public PersonBuilder withAddresses(List<Address> addresses) { | |
this.addresses = addresses; | |
return this; | |
} | |
public PersonBuilder withAddresses(Address... addresses) { | |
return withAddresses(Arrays.asList(addresses)); | |
} | |
public PersonBuilder withAddresses(AddressBuilder... addressBuilders) { | |
this.addresses = new ArrayList<Address>(); | |
for (AddressBuilder addressBuilder : addressBuilders) { | |
this.addresses.add(addressBuilder.build()); | |
} | |
return this; | |
} | |
} |
This file contains hidden or 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
@Test | |
public void hasAddress_TrueIfAPersonHasAtLeastOneAddress(){ | |
Person person = aPerson().withAddresses(anAddress()).build(); | |
assertTrue(person.hasAddress()); | |
} | |
@Test | |
public void hasAddress_FalseIfAPersonHasNoAddresses(){ | |
Person person = aPerson().withAddresses(anAddress()).build(); | |
assertFalse(person.hasAddress()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment