This class create a dummy values object. A simple usage is below.
Then you have an object all fields filled.
Customer instance = MockOjbectHelper.getDummyObject(Customer.class);
Then you have an object all fields filled.
import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import org.apache.log4j.Logger; public class MockOjbectHelper { private static final Logger LOGGER = Logger.getLogger(MockOjbectHelper.class); public static void callMethods(Object instance) { for (Method method : instance.getClass().getDeclaredMethods()) { if(isNotObjectMethod(method.getName())) { List<Object> params = new ArrayList<Object>(); System.out.print(method.getName() + " : "); for (Class<?> clazz : method.getParameterTypes()) { System.out.print(", " + clazz.getName()); Object param; Class<?> genericType = getGenericType(method.getGenericParameterTypes()); param = getDummyParameter(clazz, genericType); params.add(param); } try { method.invoke(instance, params.toArray()); } catch (IllegalAccessException e) { LOGGER.error(e.getStackTrace()); }catch (IllegalArgumentException e) { LOGGER.error(e.getStackTrace()); }catch (InvocationTargetException e) { LOGGER.error(e.getStackTrace()); } } System.out.println(); } } public static <A> A getDummyObject(Class<A> clazz){ try { A instance = clazz.newInstance(); Method[] methods = instance.getClass().getMethods(); for (Method method : methods) { if(method.getName().startsWith("set") //this is for outOfIndexException && method.getParameterTypes().length == 1) { Class<?> clz = method.getParameterTypes()[0]; Class<?> genericType = getGenericType(method.getGenericParameterTypes()); Object parameter = getDummyParameter(clz, genericType); if(parameter != null) { method.invoke(instance, parameter); } } } return instance; } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return null; } private static Class<?> getGenericType(Type[] types){ Class<?> cls = null; if(types != null && types.length > 0 && types[0] instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) types[0]; cls = (Class<?>) pType.getActualTypeArguments()[0]; } return cls; } private static Object getDummyParameter(Class<?> paramClazz, Class<?> genericType) { Object parameter = null; if(paramClazz.isArray()) { parameter = Array.newInstance(paramClazz.getComponentType(), 2); Object element = getDummyParameter(paramClazz.getComponentType(), null); Array.set(parameter, 0, element); }else if(paramClazz.equals(String.class)) { parameter = "str_dummy"; }else if(paramClazz.equals(Long.class) || paramClazz.equals(long.class)) { parameter = 123l; }else if(paramClazz.equals(Integer.class) || paramClazz.equals(int.class)) { parameter = 123; }else if(paramClazz.equals(Double.class) || paramClazz.equals(double.class)) { parameter = 357.0; }else if(paramClazz.equals(BigDecimal.class)) { parameter = new BigDecimal("135"); }else if(paramClazz.equals(Date.class)) { parameter = new Date(); }else if(paramClazz.equals(Boolean.class) || paramClazz.equals(boolean.class)) { parameter = false; }else if(paramClazz.equals(BigInteger.class)) { parameter = new BigInteger("135"); }else if(paramClazz.equals(List.class) || paramClazz.equals(ArrayList.class)) { Object obj = getDummyParameter(genericType, null); parameter = Arrays.asList(obj); }else if(paramClazz.equals(byte.class)) { parameter = new Byte("1"); }else if(paramClazz.equals(Short.class)) { parameter = new Short("1"); }else { parameter = getDummyObject(paramClazz); } return parameter; } private static boolean isNotObjectMethod(String name) { boolean result = true; for (Method method : Object.class.getMethods()) { if(method.getName().equals(name)) { result = false; break; } } return result; } public static <A> void printEntity(A instance) { try { for (Method method : instance.getClass().getMethods()) { if(method.getName().startsWith("get")) { System.out.println(method.getName() + " : " + method.invoke(instance)); } } } catch (Exception e) { LOGGER.error(e.getMessage(), e); } } }