我的任务是找出这个JUnit测试失败的原因。我发现它返回的不是ArrayList (就像getDeligationsForLoggedInUser应该返回的那样),而是返回"userList“的LinkedList。
@SuppressWarnings("unchecked")
@Test
public void test_getDelegationsForLoggedInUser()
{
String userId="Abcd";
List<String> expectedUserList= new ArrayList<String>();
expectedUserList.add("efghi");
expectedUserList.add("jklmn");
expectedUserList.add("opqrs");
when(namedParameterJdbcTemplate.queryForObject(anyString(),anyMap(), any(RowMapper.class))).thenReturn(expectedUserList);
List<String> userList= workflowProcessDAOImpl.getDelegationsForLoggedInUser(userId);
verify(namedParameterJdbcTemplate, times(1)).query(sqlCaptor.capture(), namedParameterMap.capture(), rowMapperCaptor.capture());
assertThat(userList, is(expectedUserList));
assertThat(sqlCaptor.getValue(), is(SQLConstantsSysConfigV1.getInstance().GET_USERIDS_FOR_DELEGATES));
}
有人知道为什么会这样吗?
发布于 2017-07-25 13:26:18
您在此测试中模拟方法queryForObject
,但在方法query
上进行测试。
我猜想你的代码在其他地方定义了函数的模拟,不是吗?然后,它可能只是复制/粘贴操作的结果……
发布于 2018-12-18 11:25:15
我也有同样的问题。为了解决这个问题,我替换了Mockito.anyList() (如果需要的参数是list)和Mockito.anyLong() (如果需要的参数是long)。示例:-
Mockito.when(testService.getData(Mockito.anyLong(),
Mockito.anyList(), Mockito.anyList())).thenReturn(SOME_RESPONSE_DATA);
https://stackoverflow.com/questions/45303791
复制