肖走文刀夏

导航

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();
}
}
}
暂无找到出处,如有侵权,请联系删除。

posted on 2022-05-25 13:52  肖走文刀夏  阅读(193)  评论(0编辑  收藏  举报