Mockmvc returns 清空响应主体,即使响应状态为 200

Mockmvc returns empty response body even if response status is 200

我正在尝试学习 Junit,但我遇到的情况是我的测试用例 returns 200 状态代码但 returns 空响应正文。它只是一个使用 JPA 存储库的简单保存操作,我尝试了很多在线解决方案,但 none 对我有用。

测试类:

 @RunWith(SpringRunner.class)
 @SpringBootTest
 @AutoConfigureMockMvc
 class CarManufacturingSystemApplicationTests {

   @Autowired
   private MockMvc mockMvc;

   @MockBean
   private GroupController groupController;

   ObjectMapper om = new ObjectMapper();

   @Test
   public void createGroupTest() throws Exception {
     GroupCreateRequest createRequest = new GroupCreateRequest();
     createRequest.setActiveFlag(true);
     createRequest.setGroupName("test");
     createRequest.setPlantCode(1L);

     String expectedjson = "{\r\n" + "\"message\": \"Group created successfully\"\r\n" + "}";

     MvcResult result = mockMvc.perform(post("/group/createGroup")
         .contentType(MediaType.APPLICATION_JSON_VALUE).accept(MediaType.APPLICATION_JSON_VALUE).content(new Gson().toJson(createRequest)))
       .andExpect(status().isOk())
       .andReturn();
     String actualJson = result.getResponse().getContentAsString();
     Assert.assertEquals(expectedjson, actualJson);
   }

控制器:

  @RestController
  @RequestMapping(value = "/group")
  public class GroupController {

    @Autowired
    private GroupService groupService;

    @PostMapping("/createGroup")
    public ResponseEntity<Response> createGroup(@RequestBody GroupCreateRequest groupCreateRequest) {
      Response response = groupService.createGroup(groupCreateRequest);
      return new ResponseEntity<> (response, HttpStatus.OK);
    }
}

错误:

    org.junit.ComparisonFailure: expected:<[{
    "message": "Group created successfully"
    }]> but was:<[]>
        at org.junit.Assert.assertEquals(Assert.java:115)
        at org.junit.Assert.assertEquals(Assert.java:144)
        at com.nissan.car.manufacturing.system.CarManufacturingSystemApplicationTests.createGroupTest(CarManufacturingSystemApplicationTests.java:87)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)

服务实施

public Response createGroup(GroupCreateRequest groupCreateRequest) {
        Group group = new Group();
        Response response = new Response();
        try {
            addGroupDetails(groupCreateRequest, group);
            groupRepository.save(group);

请注意,您正在测试 GroupController,而不是 GroupService,因此您应该模拟 GroupService。请替换

@MockBean
private GroupController groupController;

@MockBean
private GroupService groupService;

然后使用简单的存根指令 when(something).thenReturn(somethingElse) 使您的 groupService return 成为您指定的响应。

@Test
public void createGroupTest() throws Exception {
    // ...
    Response response = new Response();
    response.setMessage("Group created successfully");
    when(groupService.createGroup(any())).thenReturn(response);
    // ...
    Assert.assertEquals(expectedjson, actualJson);
}