我正在尝试为控制器类编写JUnit测试用例。下面的测试用例运行正常,没有任何故障。但是在那个测试用例中,我正在使用Json路径检查预期结果。我想用expected
方法检查actual
字符串结果。为此,我想知道
如何迭代AssertEquals()
结果?我想迭代这个List<Tuple>
:
queryResult
AccountController类
List<Tuple> queryResult = new ArrayList<>();
Junit测试用例:
@GetMapping("/findAccountData")
public ResponseEntity<List<Tuple>> populateGridViews(@RequestParam(value="sClientAcctId",required=false) String sClientAcctId,
@RequestParam(value="sAcctDesc",required=false) String sAcctDesc,
@RequestParam(value="sInvestigatorName",required=false)String sInvestigatorName,
@RequestParam(value="sClientDeptId",required=false) String sClientDeptId) throws Exception {
return ResponseEntity.ok(accService.populateGridViews(sClientAcctId, sAcctDesc,sInvestigatorName,sClientDeptId));
}
Account.java
@Test
@Transactional
public void populateGridViewsTest() throws Exception {
String sClientAcctId = "5400343";
String sAcctDesc = " ASTRALIS LTD";
String sInvestigatorName = "Krueger, James G.";
String sClientDeptId = "112610";
QAccount account = QAccount.account;
JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
List<Tuple> result = query.from(account).fetch();
Mockito.when(accountService.populateGridViews(sClientAcctId, sAcctDesc, sInvestigatorName, sClientDeptId))
.thenReturn(result);
mockMvc.perform(get("/spacestudy/$ InstituteIdentifier/admin/account/findAccountData")
.param("sClientAcctId", "5400343")
.param("sAcctDesc", " ASTRALIS LTD")
.param("sInvestigatorName", "Krueger, James G.")
.param("sClientDeptId", "112610")
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andExpect(jsonPath("$[0].sAcctDesc", is(" ASTRALIS LTD")))
.andExpect(jsonPath("$[0].sClientAcctId", is("5400343")))
.andExpect(jsonPath("$[0].sLocation", is("A")))
.andExpect(jsonPath("$[0].investigator.sInvestigatorName", is("Krueger, James G.")))
.andDo(print());
}
}
答案 0 :(得分:0)
例如,对每个列表进行迭代:
for (Tuple tuple : queryResult){
'put code and assertion here'
}