MockMvc 没有 return 在 post 响应中创建资源

MockMvc does not return created resource in post response

通常当您 post 到 spring 数据休息端点时,响应包含位置 header 以及 url 到新创建的资源和 json 新资源在 body.

中的表示

但是当我 post 到 MockMvc 时,如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderRestTest {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private WebApplicationContext context;

    @Autowired
    private OAuthHelper oAuthHelper;

    private MockMvc mockMvc;

    @Before
    public void setup() {

        mockMvc = MockMvcBuilders
                .webAppContextSetup(context)
                .apply(springSecurity())
                .build();

    }

    @Test
    public void testSuperuserCanCreateOrder() throws Exception {

        RequestPostProcessor accessToken = oAuthHelper.addBearerToken("someSuperUser", "ROLE_SUPERUSER");

        Order order = new Order();
        order.salesMemo = "some sales memo";

        String responseFromTestRestTemplate = objectMapper.writeValueAsString(order);
        assertNotNull(responseFromTestRestTemplate);

        mockMvc.perform(
                post("/ErPApI/orders")
                        .with(accessToken)
                        .content(objectMapper.writeValueAsString(order))
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().is2xxSuccessful());

        mockMvc.perform(
                get("/ErPApI/orders")
                        .with(accessToken))

                .andExpect(jsonPath("_embedded.orders", hasSize(1)))
                .andExpect(jsonPath("_embedded.orders[0].salesMemo", is("some sales memo")))

                .andReturn();


    }

}

post 成功,但响应 body 为空白。有没有办法用 MockMvc 模拟真实响应?我的设置有误吗?

在请求中也将 Accept header 设置为 application/json