java實體單元測試
阿新 • • 發佈:2022-05-25
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();
}
}
}
暫無找到出處,如有侵權,請聯絡刪除。