Perhaps you can use beanutils for Apache Commons to help automate this:
http://commons.apache.org/beanutils/apidocs/org/apache/commons/beanutils/PropertyUtils.html#getSimpleProperty%28java.lang.Object,java.lang.String%29
For example, there is a describe(Object bean) method that will return a map of all readable attributes (i.e. getters).
Then repeat this card and call:
setSimpleProperty(Object bean, String name, Object value)
and
public static Object getSimpleProperty(Object bean, String name)
And although I agree with another poster, since getters / seters are pretty trivial - I think it’s worth testing them anyway - eliminate typos, listen to changes in test properties, etc.
For example, this will dynamically extract bean getters:
import java.io.Serializable; import java.util.Set; import org.apache.commons.beanutils.PropertyUtils; public class MyTestBean implements Serializable { private int a; private int b; private int c; private String x; private String y; private String z; public static void main(String[] args) throws Exception { MyTestBean bean=new MyTestBean(); Set prop=PropertyUtils.describe(bean).keySet(); for (Object o : prop) { System.out.println((String)o); } } public int getA() { return a; } public void setA(int a) { this.a = a; } public int getB() { return b; } public void setB(int b) { this.b = b; } public int getC() { return c; } public void setC(int c) { this.c = c; } public String getX() { return x; } public void setX(String x) { this.x = x; } public String getY() { return y; } public void setY(String y) { this.y = y; } public String getZ() { return z; } public void setZ(String z) { this.z = z; }}
To run this code you need to load both BeanUtils, CommonsLogging and JAR for both libraries into your project.
monojohnny
source share