Builder pattern is a simple alternate to setup unit test data using reflection without exposing unnecessary setters and constructors in domain objects.
1) Abstract builder:
Create an Abstract builder that will be extended by all domain builder objects.
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AbstractBuilder<E> {
private E domain;
public AbstractBuilder(E domain) {
this.domain = domain;
}
public E build() {
try {
List<Field> domainFields = getAllFields(new ArrayList<Field>(), domain.getClass());
Map<String, Field> builderFieldsMap = index(getAllFields(new ArrayList<Field>(), this.getClass()));
for (Field domainField : domainFields) {
Field builderField = builderFieldsMap.get(domainField.getName());
if (builderField != null) {
copyFieldValue(domainField, builderField);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return domain;
}
private void copyFieldValue(Field domainField, Field builderField) throws IllegalAccessException {
domainField.setAccessible(true);
builderField.setAccessible(true);
domainField.set(domain, builderField.get(this));
}
private Map<String, Field> index(List<Field> allFields) {
Map<String, Field> map = new HashMap<String, Field>();
for (Field allField : allFields) {
map.put(allField.getName(), allField);
}
return map;
}
public static List<Field> getAllFields(List<Field> fields, Class<?> type) {
for (Field field : type.getDeclaredFields()) {
fields.add(field);
}
if (type.getSuperclass() != null) {
fields = getAllFields(fields, type.getSuperclass());
}
return fields;
}
public E getDomain() {
return domain;
}
}
2) Domain builder class:
Consider a domain class Person with 4 attributes (firstName, lastName, gender, dateOfBirth). You should create the domain builder object with same 4 attributes.
public class PersonBuilder extends AbstractBuilder<Person> {
private String firstName;
private String lastName;
private String gender;
private Date dateOfBirth;
public PersonBuilder() {
super(new Person());
}
public PersonBuilder withFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public PersonBuilder withLastName(String lastName) {
this.lastName = lastName;
return this;
}
public PersonBuilder withGender(String gender) {
this.gender = gender;
return this;
}
public PersonBuilder withDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
return this;
}
}
3) Using Builders in Unit Tests:
You can use the builder as shown below
Person testPerson = new PersonBuilder().withFirstName("hello").withLastName("World").build();
Tip:
If you are using Intellij IDEA as the IDE, use Builder plugin to generate the "with" methods quickly.
You rock ! Wow. This helped big time in writing my domain tests.
ReplyDelete-Sriram Narasimhan
Thanks for posting....makes creating builders so much less work.
ReplyDeleteGreat Article
ReplyDeleteJava Projects for Engineering Students of Computer Science
Java Training in Chennai
Project Centers in Chennai for CSE
Java Training in Chennai
rastgele görüntülü konuşma - kredi hesaplama - instagram video indir - instagram takipçi satın al - instagram takipçi satın al - tiktok takipçi satın al - instagram takipçi satın al - instagram beğeni satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - instagram takipçi satın al - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - binance güvenilir mi - instagram beğeni satın al - instagram beğeni satın al - polen filtresi - google haritalara yer ekleme - btcturk güvenilir mi - binance hesap açma - kuşadası kiralık villa - tiktok izlenme satın al - instagram takipçi satın al - sms onay - paribu sahibi - binance sahibi - btcturk sahibi - paribu ne zaman kuruldu - binance ne zaman kuruldu - btcturk ne zaman kuruldu - youtube izlenme satın al - torrent oyun - google haritalara yer ekleme - altyapısız internet - bedava internet - no deposit bonus forex - erkek spor ayakkabı - webturkey.net - karfiltre.com - tiktok jeton hilesi - tiktok beğeni satın al - microsoft word indir - misli indir
ReplyDeletetiktok jeton hilesi
ReplyDeletetiktok jeton hilesi
binance referans kimliği
gate güvenilir mi
tiktok jeton hilesi
paribu
btcturk
bitcoin nasıl alınır
İnstagram takipçi satın al! İnstagram takipçi sitesi ile takipçi satın al sende sosyal medyada fenomen olmaya bir adım at. Sende hemen instagram takipçi satın almak istiyorsan tıkla:
ReplyDelete1- takipçi satın al
2- takipçi satın al
3- takipçi satın al
Thanks for all you do. I like the website themes and layout, I follow you with joy and I respect your articles. travel health surveillance form Kenya Is necessary for all the travelers who want to visit Kenya . Because it is compulsory by the government of Kenya .
ReplyDelete