我在我的项目中使用hibernate,下面是我的java代码。
public class TestInstancesDaoImpl implements TestInstancesDao {
private final static String sqlQueryForTestInstances =
"SELECT tc.testCycleId,tc.testId,tc.status,
tc.executionDate,tc.executionTime,tc.testInstanceNumber,
tc.testerName,tc.testId,t.testName,t.testType,
tc.analysis from TestInstance tc, Test t where tc.cycleId = :cycleId"
"AND tc.testId = t.testId ORDER BY tc.executionDate desc,tc.executionTime desc";
public List<TestInstance> getAllTestInstances(String domainName,
String projectName, Long testSetId,String server) throws CustomException {
String dbName = almDatabaseDao.getDBName(domainName, projectName, server);
List<TestInstance> testInstanceList = new ArrayList<TestInstance>();
List<?> list = null;
Transaction tx = null;
try {
tx = session.beginTransaction();
Query q = session.createQuery(sqlQueryForTestInstances);
q.setParameter("cycleId", testSetId);
list = q.list();
StringBuilder testCycleIds = new StringBuilder();
ListIterator<?> it = list.listIterator();
while (it.hasNext()) {
Object[] testInstanceObject = (Object[]) it.next();
if (testInstanceObject[0] != null) {
int testCycleId =Integer.parseInt(testInstanceObject[0].toString());
testInstance.setTestCycleId(testCycleId);
testInstanceList.add(testInstance);
}
}
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return testInstanceList;
}
下面是我的单元测试代码。 抛出错误 - java.lang.ClassCastException:java.util.ArrayList在迭代列表时无法强制转换为[Ljava.lang.Object; Object [] testInstanceObject =(Object [])it.next();
从hibernate返回的列表将以下面的结构形式 - [[Ljava.lang.Object; @ 79e2764b,[Ljava.lang.Object; @ 2c47471]]
如何构建上面的类似于hibernate返回的列表的模拟列表?Cananyone如何帮助我解决问题?
@Test
public void testGetAllTestInstancesImpl(){
String domain = "XXX";
String server = "XXX";
String projectName = "XXX";
String dbName = "XXXX";
Object [][] a = {{1,1,"Blocked","20160622","12:29:23",1,"",1,"XXX","MANUAL","test 404"}};
Long testSetId = 1L;
List<?> defectList = null;
ArrayList<List<?>> alist = new ArrayList<List<?>>(1);
List<List<?>> list = alist;
List<?> listNew = twoDArrayToList(a);
list.add(listNew);
ListIterator<?> it = list.listIterator();
List<TestInstance> testList = getTestInstanceList();
TestInstancesDaoImpl testInstancesDaoImpl = new TestInstancesDaoImpl();
testInstancesDaoImpl.setALMDatabaseDao(almDatabaseDao);
PowerMockito.mockStatic(HibernateUtil.class);
when(almDatabaseDao.getDBName("XXX", "XX", "Staging")).thenReturn(dbName);
when(HibernateUtil.getSession(dbName, "Staging")).thenReturn(session);
when(session.beginTransaction()).thenReturn(transaction);
when(session.createQuery(sqlQueryForTestInstances)).thenReturn(q);
when(q.setParameter("cycleId", testSetId)).thenReturn(q);
when(q.list()).thenReturn(list);
when(session.createQuery(sqlQueryForDefects)).thenReturn(query);
when(query.setParameter("cycleId", testSetId)).thenReturn(query);
List<TestInstance> testInstanceList= testInstancesDaoImpl.getAllTestInstances(domain, projectName, id, server);
}