Junit 测试用例:java.lang.exception:json 不能为 null 或为空
Junit Test case : java.lang.exception: json can not be null or empty
我正在尝试使用 JUnit
和 mockito
为控制器编写测试用例。但是我收到错误消息,因为 JSON 不能为 null 或为空。任何人都可以告诉我我做错了什么吗?
部门主管
@RestController
@RequestMapping("/api.spacestudy.com/SpaceStudy")
public class DepartmentController {
@Autowired
DepartmentService depService;
@GetMapping("/Control/SearchFilter/loadDepartments")
public ResponseEntity<Set<Department>> findDepName() {
Set<Department> depname = depService.findDepName();
return ResponseEntity.ok(depname);
}
DepartmentControllerTest
@RunWith(SpringJUnit4ClassRunner.class)
public class DepartmentControllerTest {
private MockMvc mockMvc;
@Mock
public DepartmentService depService;
@InjectMocks
DepartmentController departmentController;
@Before
public void setup() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(departmentController).build();
}
@Test(timeout = 10000)
public void findDepNameTest() throws Exception
{
Department dep = new Department();
dep.setsDeptName("ABC");
Set<Department> department = new HashSet<Department>();
department.add(dep);
Mockito.when(depService.findDepName()).thenReturn(department);
mockMvc.perform(get("/api.spacestudy.com/SpaceStudy/LoadDept").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].sDeptName" , is("abc")));
}
}
故障跟踪
java.lang.AssertionError: No value at JSON path "$[0].sDeptName", exception: json can not be null or empty
at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:245)
at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:73)
at org.springframework.test.web.servlet.result.JsonPathResultMatchers.match(JsonPathResultMatchers.java:87)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:171)
at com.spacestudy.DepartmentControllerTest.findDepNameTest(DepartmentControllerTest.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
我发现了我的错误。我在上面的代码中为获取请求提供了错误的路径。我更改了该路径并使其正常工作。
mockMvc.perform(get("/api.spacestudy.com/SpaceStudy/Control/SearchFilter/loadDepartmentsLoadDept").accept(MediaType.APPLICATION_JSON))
我正在尝试使用 JUnit
和 mockito
为控制器编写测试用例。但是我收到错误消息,因为 JSON 不能为 null 或为空。任何人都可以告诉我我做错了什么吗?
部门主管
@RestController
@RequestMapping("/api.spacestudy.com/SpaceStudy")
public class DepartmentController {
@Autowired
DepartmentService depService;
@GetMapping("/Control/SearchFilter/loadDepartments")
public ResponseEntity<Set<Department>> findDepName() {
Set<Department> depname = depService.findDepName();
return ResponseEntity.ok(depname);
}
DepartmentControllerTest
@RunWith(SpringJUnit4ClassRunner.class)
public class DepartmentControllerTest {
private MockMvc mockMvc;
@Mock
public DepartmentService depService;
@InjectMocks
DepartmentController departmentController;
@Before
public void setup() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(departmentController).build();
}
@Test(timeout = 10000)
public void findDepNameTest() throws Exception
{
Department dep = new Department();
dep.setsDeptName("ABC");
Set<Department> department = new HashSet<Department>();
department.add(dep);
Mockito.when(depService.findDepName()).thenReturn(department);
mockMvc.perform(get("/api.spacestudy.com/SpaceStudy/LoadDept").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$[0].sDeptName" , is("abc")));
}
}
故障跟踪
java.lang.AssertionError: No value at JSON path "$[0].sDeptName", exception: json can not be null or empty
at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:245)
at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:73)
at org.springframework.test.web.servlet.result.JsonPathResultMatchers.match(JsonPathResultMatchers.java:87)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:171)
at com.spacestudy.DepartmentControllerTest.findDepNameTest(DepartmentControllerTest.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
我发现了我的错误。我在上面的代码中为获取请求提供了错误的路径。我更改了该路径并使其正常工作。
mockMvc.perform(get("/api.spacestudy.com/SpaceStudy/Control/SearchFilter/loadDepartmentsLoadDept").accept(MediaType.APPLICATION_JSON))