我需要使用mockito框架为VoltDb程序编写测试用例。数据库返回作为数组的表,我需要从表中使用索引号得到表,然后迭代行,从列中获取值。所以,如果我可以得到一个例子,在嘲讽voltdb连接,表和结果,这将是巨大的。
Client voltDbClient;
@Mock
DAOFactory daoFactory;
@Test
public void testGetECMPWaitOverSelect() throws Exception {
String procedure = "Procedure Name is here";
int offset = PropertiesLoader.getIntValue(PropertiesLoader.OFFSET);
int fetchlimit = PropertiesLoader.getIntValue(PropertiesLoader.WAITOVER_FETCH_LIMIT);
Mockito.when(voltDbClient.callProcedure(procedure, offset,fetchlimit)).thenReturn(voltDbClient.callProcedure(procedure, offset, fetchlimit));
VoltTable[] voltTable = voltDbClient.callProcedure(procedure, offset, fetchlimit).getResults();
Mockito.verify(voltDbClient,Mockito.times(1)).callProcedure(procedure, offset, fetchlimit);
}
我希望表能得到模拟的结果,因为类被模拟,并验证db过程是否执行,但由于它不是正确的方式来做,我没有得到适当的结果。
在你的when(funcA()).thenReturn(funcA())中,你似乎在返回同一个函数调用。
你可能需要更多的东西,比如:-
when(client.callProcedure("FOO.select", id)).thenReturn(new ClientResponse() {
@Override
public byte getStatus() {
return ClientResponse.SUCCESS;
}
@Override
public byte getAppStatus() {
return 0;
}
@Override
public VoltTable[] getResults() {
VoltTable statusRow = new VoltTable(
new VoltTable.ColumnInfo("StatusId", VoltType.INTEGER),
new VoltTable.ColumnInfo("LAST_UPDATED", VoltType.TIMESTAMP),
new VoltTable.ColumnInfo("VAL", VoltType.STRING)
);
statusRow.addRow(1, new TimestampType(new Date()), "Hello again");
return new VoltTable[]{statusRow};
}
@Override
public String getStatusString() {
return null;
}
@Override
public String getAppStatusString() {
return null;
}
@Override
public int getClusterRoundtrip() {
return 0;
}
@Override
public int getClientRoundtrip() {
return 0;
}
@Override
public long getClientRoundtripNanos() {
return 0;
}
});
response = client.callProcedure("FOO.select", id);
VoltTable t = response.getResults()[0];
assertEquals(t.getRowCount(), 1);
t.advanceRow();
long lastUpdatedMicros = t.getTimestampAsLong("LAST_UPDATED");
long initialDateMicros = initialDate.getTime() * 1000;
assertTrue(lastUpdatedMicros > initialDateMicros);
String latestVal = t.getString("VAL");
assertEquals(latestVal, val);
对不起,这是一个非常晚的回应,但也许这可能会帮助你或其他人,正如评论中所说的,确实引出了一个问题,你试图测试什么。