1.实现Iterator<>接口,初始化参数传入对应的sql和测试数据的class对象
package com.styd.utils.common;
import com.styd.utils.HttpBase;
import javax.persistence.Query;
import java.util.Iterator;
import java.util.List;
public class DataProviderFactory implements Iterator<Object>{
public String sql;
public int length;
public List<Object> list;
public int cursor = -1;
public DataProviderFactory(String sql,Class clazz){
this.sql = sql;
Query query = HttpBase.dataBaseUtil.entityManager.createNativeQuery(sql,clazz);
list = query.getResultList();
length = list.size();
}
@Override
public boolean hasNext() {
return cursor+1 < length;
}
@Override
public Object next() {
cursor ++;
return list.get(cursor);
}
}
2.实现具体的dataprovider,返回DataProviderFactory对象
@DataProvider(name = "memberCardInfo")
public Iterator<Object> dataProvider(){
String sql = "select * from CreateShopMemberCard";
return new DataProviderFactory(sql,CreateShopMemberCardBean.class);
}