Created
February 28, 2012 16:00
-
-
Save darrend/1933334 to your computer and use it in GitHub Desktop.
a generic builder
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.lang.reflect.Field; | |
/** | |
* Utility method for quickly building mock models | |
*/ | |
public class Builder { | |
public static <T> T build(Class<T> clazz, Object... keyValues) { | |
try { | |
return build(clazz, clazz.newInstance(), keyValues); | |
} catch (InstantiationException e) { | |
throw new RuntimeException(e); | |
} catch (IllegalAccessException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public static <T> T build(Class<T> clazz, T instance, Object[] keyValues) { | |
String key = null; | |
for (Object keyValue : keyValues) { | |
if(key==null) { | |
key = (String) keyValue; | |
} else { | |
Field declaredField; | |
try { | |
declaredField = clazz.getDeclaredField(key); | |
} catch (NoSuchFieldException e) { | |
throw new RuntimeException(e); | |
} | |
declaredField.setAccessible(true); | |
try { | |
declaredField.set(instance, keyValue); | |
} catch (IllegalAccessException e) { | |
throw new RuntimeException(e); | |
} | |
key = null; | |
} | |
} | |
return instance; | |
} | |
} |
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 org.testng.Assert; | |
import org.testng.annotations.Test; | |
import static com.papajohns.test.mock.Builder.build; | |
public class BuilderTest { | |
@Test | |
public void testBuilder() { | |
Person person = build(Person.class); | |
Assert.assertNull(person.getName()); | |
person = build(Person.class, "id",1001, | |
"name", build(Name.class, "firstName", "Darren", "lastName","Day") | |
,"address",build(Address.class,"street","106 Birch Street","city","Tricity","state","Euphoria") | |
,"birthDate","March 1, 1900"); | |
Assert.assertEquals(person.getId(),1001); | |
Assert.assertEquals(person.getName().getFirstName(),"Darren"); | |
Assert.assertEquals(person.getBirthDate(),"March 1, 1900"); | |
Assert.assertEquals(person.getAddress().getState(),"Euphoria"); | |
} | |
public static class Name { | |
private String firstName; | |
private String lastName; | |
public String getFirstName() { | |
return firstName; | |
} | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
} | |
public static class Address { | |
private String street; | |
private String city; | |
private String state; | |
public String getStreet() { | |
return street; | |
} | |
public void setStreet(String street) { | |
this.street = street; | |
} | |
public String getCity() { | |
return city; | |
} | |
public void setCity(String city) { | |
this.city = city; | |
} | |
public String getState() { | |
return state; | |
} | |
public void setState(String state) { | |
this.state = state; | |
} | |
} | |
public static class Person { | |
private int id; | |
private Name name; | |
private Address address; | |
private String birthDate; | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public Name getName() { | |
return name; | |
} | |
public void setName(Name name) { | |
this.name = name; | |
} | |
public Address getAddress() { | |
return address; | |
} | |
public void setAddress(Address address) { | |
this.address = address; | |
} | |
public String getBirthDate() { | |
return birthDate; | |
} | |
public void setBirthDate(String birthDate) { | |
this.birthDate = birthDate; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment