簡體   English   中英

Spring MockMvc 返回 200 而不是 201。如何調試?

[英]Spring MockMvc is returning 200 instead of a 201. How to debug?

我正在嘗試使用 Spring MockMvc 測試具有多個 HTTP 動詞的控制器。 目前GETDELETE按預期工作,但PUTPATCH沒有返回 201,而它們應該返回。

PATCH控制器設置如下:

@CustomRequestMapping(method = PATCH, value = "/{api-root}")
public ResponseEntity patch(@PathVariable(value = "api-root") String id, @RequestBody ApiRoot apiRoot) {
     return apiRootService.softUpdate(id, apiRoot);
}

@CustomRequestMapping...注釋只是將consumesproduces設置為特定的內容類型。

softUpdate()方法執行以下操作:

public ResponseEntity softUpdate(String id, ApiRoot apiRoot) {
    if (apiRoot.getId() == null) {
        apiRoot.setId(id);
    }

    ApiRootDocument updated = softUpdate(apiRoot);
    return ResponseEntity
            .created(EscapeUtil.buildUrl(applicationProperties.getHostname(), applicationProperties.getPort(), id))
            .body(updated);
}

這是在 MockMvc 單元測試之外工作和測試的。 它正確地將201 Created返回給 Postman,主體是作為PATCH結果創建的新 JSON 對象。

我的測試設置為:

public void testPatchApiRootEndpoint() throws Exception {
    String testTitle = "New Test Title";

    // Mock the service call for softUpdate() to return 'created' in the same way that the method does
    when(apiRootService.softUpdate(TestData.apiRoot1.getId(), TestData.apiRoot1.withTitle(testTitle)))
            .thenReturn(ResponseEntity
                .created(URI.create(EscapeUtil.buildUrlString("localhost", "8001", TestData.apiRoot1.getId())))
                .body((ApiRootDocument) TestData.apiRoot1.withTitle(testTitle)));

    // Perform a patch update using a new title provided as key-value
    JsonObject titleJson = new JsonObject();
    titleJson.addProperty("title", testTitle);
    mockMvc.perform(patch("/{api-root}", TestData.apiRoot1.getId())
                .contentType(Constants.TAXII2_CONTENT_TYPE)
                .content(titleJson.toString()))
            .andExpect(status().isCreated());
}

這導致200被返回,而不是201 我真的很困惑,一直很難研究。 大多數類似的問題是在他們期望 2XX 時找到 4XX 響應代碼,解決方案通常與請求的設置有關。

我似乎能找到的唯一有趣的日志是:

09:48:25.064 [main] DEBUG org.springframework.test.web.servlet.TestDispatcherServlet - Servlet '' configured successfully
09:48:25.208 [main] DEBUG org.springframework.test.web.servlet.TestDispatcherServlet - DispatcherServlet with name '' processing PATCH request for [/api-root-1]
09:48:25.211 [main] DEBUG org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping - Looking up handler method for path /api-root-1
09:48:25.219 [main] DEBUG org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder$StaticRequestMappingHandlerMapping - Returning handler method [public org.springframework.http.ResponseEntity xor.bcmc.flarecloud.apiroot.controller.ApiRootController.patch(java.lang.String,xor.bcmc.taxii2.resources.ApiRoot)]
09:48:25.388 [main] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor - Read [class xor.bcmc.taxii2.resources.ApiRoot] as "application/vnd.oasis.taxii+json;version=2.0" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@20921b9b]
09:48:25.428 [main] DEBUG org.springframework.test.web.servlet.TestDispatcherServlet - Null ModelAndView returned to DispatcherServlet with name '': assuming HandlerAdapter completed request handling
09:48:25.428 [main] DEBUG org.springframework.test.web.servlet.TestDispatcherServlet - Successfully completed request

看起來您的模擬電話無效。 您可以輕松檢查是否將值替換為 any():

when(apiRootService.softUpdate(Mockito.any(), Mockito.any())
.thenReturn(ResponseEntity
                .created(URI.create(EscapeUtil.buildUrlString("localhost", "8001", TestData.apiRoot1.getId())))
                .body((ApiRootDocument) TestData.apiRoot1.withTitle(testTitle)));

我剛剛檢查了您是否使用了錯誤的控制器模擬返回 200 狀態,但如果模擬正確,我得到 201。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM