java.lang.AssertionError:JSON路径“ $ [0] .sAcctDesc”

时间:2018-07-13 14:18:30

标签: spring-data-jpa mockito junit4 querydsl

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

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

谁能告诉我为什么我的身体变得空虚?

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

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

控制台

在这里,即使正文为空,我仍然可以接收查询结果。谁能告诉我为什么那个尸体是[]?

    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 = []

1 个答案:

答案 0 :(得分:0)

您的结果在这里为空:

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现在将跳过真实的方法调用,并立即返回之前的空结果数组。