Я пытаюсь написать тестовый пример JUnit для класса контроллера.Тестовый пример работает правильно, без каких-либо сбоев.Но в этом тестовом примере я проверяю ожидаемый результат, используя путь Json.Я хочу проверить expected
строковый результат с actual
строковым результатом, используя AssertEquals()
метод.Для этого я хочу знать
Как перебрать List<Tuple>
результат?Я хочу повторить этот queryResult
:
List<Tuple> queryResult = new ArrayList<>();
Класс AccountController
@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));
}
Тестовый пример Junit:
@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());
}
}
Account.java
@Entity
@Table(name = "account")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "account_seq_generator")
@SequenceGenerator(name = "account_seq_generator", sequenceName = "account_seq")
@Column(name = "naccount_id")
public Integer nAccountId;
@Column(name = "namount")
public String nAmount;
@Column(name = "sacct_desc")
public String sAcctDesc;
@Column(name = "naccount_cpc_mapping_id")
public Integer nAccountCPCMappingId;
@Column(name = "nindirect_cost_rate")
public Integer nIndiretCostRate;
@Column(name = "nagency_id")
public Integer nAgencyId;
@Column(name = "ndept_id")
public Integer nDeptId;
@Column(name = "sgrant_num")
public String sGrantNum;
@Column(name = "dstart_date")
public Timestamp dStartDate;
@Column(name = "dend_date")
public Timestamp dEndDate;
@Column(name = "slocation")
public String sLocation;
@Column(name = "sclient_acct_id")
public String sClientAcctId;
@Column(name = "ninvestigator_id")
public Integer nInvestigatorId;
@Column(name = "ninst_id")
public Integer nInstId;
@Column(name = "ntemp_account_id")
public Integer nTempAccountId;
@ManyToOne(optional = true, cascade = { CascadeType.MERGE })
@JoinColumn(name = "ndept_id", insertable = false, updatable = false)
public Department department;
@ManyToOne(optional = true, cascade = { CascadeType.ALL })
@JoinColumn(name = "ninvestigator_id", insertable = false, updatable = false)
public Investigator investigator;
@ManyToOne(optional = true, cascade = { CascadeType.ALL })
@JoinColumn(name = "naccount_cpc_mapping_id", insertable = false, updatable = false)
public AccountCPCMapping accountCPC;