java实体单元测试
作者:互联网
import org.junit.Test;
import java.beans.Beans;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public abstract class EntityTest<E> {
protected abstract E getE();
private void execute() throws IllegalAccessException, InstantiationException {
E e=getE();
Class aClass = e.getClass();
Object o = aClass.newInstance();
Field[] declaredFields = aClass.getDeclaredFields();
for (Field f: declaredFields
) {
try{
if(f.isSynthetic()){
continue;
}
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(f.getName(), aClass);
Method readMethod = propertyDescriptor.getReadMethod();
Method writeMethod = propertyDescriptor.getWriteMethod();
writeMethod.invoke(o,readMethod.invoke(o));
}catch (Exception exc) {
continue;
}
}
}
@Test
public void invoke() throws InstantiationException, IllegalAccessException {
this.execute();
}
public class BeansTest extends EntityTest<Beans>{
@Override
protected Beans getE() {
return new Beans();
}
}
}
暂无找到出处,如有侵权,请联系删除。
标签:aClass,java,实体,单元测试,getE,Field,import,Method 来源: https://www.cnblogs.com/xzwdx/p/16309061.html