JSON路径$[0]处没有值。名称?
我目前正在开发一个rest api web服务,我必须使用单元测试对其进行测试,因此我不知道如何使用Mockito和Junit使用Spring测试rest api,首先我已经准备好了类,在该类中我创建了两个方法@Before和@test,当我在调试模式下打开控件中的方法时“getEmployeDTOList”始终返回null,因此控制台显示异常NullPointerException 类EmployeControllerTest:JSON路径$[0]处没有值。名称?,json,spring-boot,junit,mockito,hamcrest,Json,Spring Boot,Junit,Mockito,Hamcrest,我目前正在开发一个rest api web服务,我必须使用单元测试对其进行测试,因此我不知道如何使用Mockito和Junit使用Spring测试rest api,首先我已经准备好了类,在该类中我创建了两个方法@Before和@test,当我在调试模式下打开控件中的方法时“getEmployeDTOList”始终返回null,因此控制台显示异常NullPointerException 类EmployeControllerTest: @RunWith(SpringJUnit4ClassRunner
@RunWith(SpringJUnit4ClassRunner.class)
public class EmployeControllerTest {
private MockMvc mockMvc;
@InjectMocks
private EmployeController employeController ;
@Mock
private EmployeService employeService ;
@Mock
IConverter converterDto ;
@Before
public void setUp() throws Exception{
MockitoAnnotations.initMocks(this);
mockMvc=MockMvcBuilders.standaloneSetup(employeController).build();
}
@Test
public void testgetAllEmployee() throws Exception{
List<Employe> employes= Arrays.asList(
new Employe("BOUROUNIA", "HICHEM", "h.bourounia", "9dfd1be37de137f146ca990310a1483c",true)
,new Employe("BRADAI", "ALI", "a.bradai", "830ddf1b7e8e34261f49d20e5e549338",true) );
when(employeService.findAll()).thenReturn(employes);
mockMvc.perform(get("/employe/dto"))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].nom", is("BOUROUNIA")))
.andExpect(jsonPath("$[0].prenom", is("HICHEM")))
.andExpect(jsonPath("$[0].login", is("h.bourounia")))
.andExpect(jsonPath("$[0].mp", is("9dfd1be37de137f146ca990310a1483c")))
.andExpect(jsonPath("$[0].actif", is(true)))
.andExpect(jsonPath("$[1].nom", is("BRADAI")))
.andExpect(jsonPath("$[1].prenom", is("ALI")))
.andExpect(jsonPath("$[1].login", is("a.bradai")))
.andExpect(jsonPath("$[1].mp", is("830ddf1b7e8e34261f49d20e5e549338")))
.andExpect(jsonPath("$[1].actif", is(true))).andReturn().getResponse().getContentAsString();
verify(employeService,times(1)).findAll();
verifyNoMoreInteractions(employeService);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
公共类EmployeControllerTest{
私有MockMvc-MockMvc;
@注射模拟
私人雇员控制员雇员控制员;
@嘲弄
私人就业服务;
@嘲弄
i转换器转换器至;
@以前
public void setUp()引发异常{
initMocks(this);
mockMvc=MockMvcBuilders.standaloneSetup(employeController.build();
}
@试验
public void testgetAllEmployee()引发异常{
List employes=Arrays.asList(
新员工(“BOUROUNIA”、“HICHEM”、“h.BOUROUNIA”、“9dfd1be37de137f146ca990310a1483c”,真实)
,新雇员(“BRADAI”、“ALI”、“a.BRADAI”、“830ddf1b7e8e34261f49d20e549338”,真));
when(employeeservice.findAll())。然后返回(employes);
mockMvc.perform(get(“/employe/dto”))
.andExpect(状态().isOk())
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION\u JSON\u UTF8\u VALUE))
.andExpect(jsonPath(“$”,hasSize(2)))
.andExpect(jsonPath(“$[0].nom”,是(“BOUROUNIA”))
.andExpect(jsonPath(“$[0].prenom”,is(“HICHEM”))
.andExpect(jsonPath(“$[0].login”),是(“h.bourounia”))
.andExpect(jsonPath(“$[0].mp”)是(“9dfd1be37de137f146ca990310a1483c”))
.andExpect(jsonPath(“$[0].actif”,为(true)))
.andExpect(jsonPath(“$[1].nom”,即“BRADAI”))
.andExpect(jsonPath(“$[1].prenom”,is(“ALI”))
.andExpect(jsonPath(“$[1].login”),是(“a.bradai”))
.andExpect(jsonPath(“$[1].mp”)是(“830DDF1B7E8E34261F49D20E549338”))
.andExpect(jsonPath(“$[1].actif”,is(true)).andReturn().getResponse().getContentAsString();
验证(EmployeeService,次(1)).findAll();
验证Nomore交互(员工服务);
}
}
这是Employecontroller:
@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
@RequestMapping("/employe")
public class EmployeController {
@GetMapping("/dto")
public List<EmployeDTO > getEmployeDTOList(){
try {
List<Employe> listemp=employeService.findAll();
return listemp.stream()
.filter(Objects::nonNull)
.map(emp ->converterDTO.convertToDto(emp))
.collect(Collectors.toList());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
@CrossOrigin(origins=“*”,allowedHeaders=“*”)
@RestController
@请求映射(“/employe”)
公共类EmployeController{
@GetMapping(“/dto”)
公共列表getEmployeDTOList(){
试一试{
List listemp=employeeservice.findAll();
返回listemp.stream()
.filter(对象::非空)
.map(emp->converterDTO.convertToDto(emp))
.collect(Collectors.toList());
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
}
我得到的错误:
java.lang.AssertionError:JSON路径“$[0].id”处没有值:
com.jayway.jsonpath.PathNotFoundException:应查找对象
在路径$[0]中具有属性['id'],但找到“null”。这不是json
根据
JsonProvider:'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'
我还不熟悉rest api web服务和单元测试。根据我的经验,jsonPath不应该是
jsonPath($.employes[0].mp”)
而不是jsonPath($[0].mp”)。
原因是,因为它返回的是ArrayList,所以必须指明保存数据的对象。
我使用的语法是$.ArrayListName[elementPosition].columnName