gpt4 book ai didi

spring-data-jpa - java.lang.AssertionError : No value at JSON path "$[0].sAcctDesc" 错误

转载 作者:行者123 更新时间:2023-12-01 01:30:56 27 4
gpt4 key购买 nike

我正在尝试为帐户 Controller 类编写测试用例。帐户 Controller 取决于帐户服务类,因此我使用的是 mockito。在帐户服务中,我使用 Querydsl 创建动态查询以实现搜索过滤器。

我尝试编写如下测试用例。在该测试类中,我能够以数组格式接收查询结果。但是我的 body 变空了,因此出现了这个错误 java.lang.AssertionError: No value at JSON path "$[0].sAcctDesc"

谁能告诉我为什么我的 body 越来越空?

账户 Controller

    @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));
}

TestAccountControllerGrid

    @RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class TestAccountControllerGrid {

@Autowired
private MockMvc mockMvc;

@Mock
private AccountService accountService;

@InjectMocks
private AccountController accountController;

@Autowired
EntityManager em;

@Value("${InstituteIdentifier}")
private String instituteIdentifier;

@Test
@Transactional
public void populateGridViewsTest() throws Exception {

String sClientAcctId ="5400343";
String sAcctDesc =" ASTRALIS LTD";
String sInvestigatorName ="Krueger, James G.";
String sClientDeptId ="112610";


Department departmentObject = new Department();
departmentObject.setsClientDeptId("112610");
departmentObject.setsDeptName("Konarska Laboratory");

Investigator investigatorObject= new Investigator();
investigatorObject.setsInvestigatorName("Krueger, James G.");

Account accountObject = new Account();
accountObject.setsAcctDesc(" ASTRALIS LTD");
accountObject.setsClientAcctId("5400343");
accountObject.setsLocation("A");
accountObject.setDepartment(departmentObject);
accountObject.setInvestigator(investigatorObject);

em.merge(accountObject);

QAccount account = QAccount.account;

JPAQuery<Tuple> query = new JPAQuery<Tuple>(em);
List<Tuple> result = query.select(account.sAcctDesc, account.sClientAcctId,account.sLocation)
.from(account).fetch();

System.out.println("Query Result = "+ result);

Mockito.when(accountService.populateGridViews(sClientAcctId, sAcctDesc, sInvestigatorName, sClientDeptId))
.thenReturn(result).toString();


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].sClientAcctId", is("5400343")))
.andExpect(jsonPath("$[0].sClientAcctId", is("5400343")))
.andExpect(jsonPath("$[0].sLocation", is("A")))
.andExpect(jsonPath("$[0].department.sDeptName", is("Konarska Laboratory")))
.andExpect(jsonPath("$[0].investigator.sInvestigatorName", is("Krueger, James G.")));

Mockito.verify(accountService).populateGridViews(sClientAcctId, sAcctDesc, sInvestigatorName, sClientDeptId);
}
}

控制台

在这里,我仍然能够收到主体为空的查询结果。任何人都可以告诉我为什么那个 body 是 [] 吗?

    Query Result = [[ ASTRALIS LTD, 5400343, A]]

MockHttpServletRequest:
HTTP Method = GET
Request URI = /spacestudy/rockefeller/admin/account/findAccountData
Parameters = {sClientAcctId=[5400343], sAcctDesc=[ ASTRALIS LTD], sInvestigatorName=[Krueger, James G.], sClientDeptId=[112610]}
Headers = {Accept=[application/json]}
Body = null
Session Attrs = {}

MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = []

最佳答案

你的结果在这里是空的:

System.out.println("result "+result); // getting empty []

在下一行中,您为 Mockito 指定当调用 AccountService 方法 populateGridViews(...) 时您希望返回空结果数组:

Mockito.when(accountService.populateGridViews(sClientAcctId, sAcctDesc, sInvestigatorName, sClientDeptId))
.thenReturn(result);

然后 mockMvc.perform(...) 方法调用您的端点:

@GetMapping("/findAccountData")
public ResponseEntity<List<Tuple>> populateGridViews(...){...}

此方法调用 AccountService 方法 populateGridViews(...) :

accService.populateGridViews(...)

Mockito 现在将跳过真正的方法调用并立即返回之前的空结果数组。

关于spring-data-jpa - java.lang.AssertionError : No value at JSON path "$[0].sAcctDesc" 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51327261/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com