本文主要是介绍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();
}
}
}
暂无找到出处,如有侵权,请联系删除。
这篇关于java实体单元测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!