繁体   English   中英

在春季测试5.0.3中无法在MockMvc异步REST服务上测试HTTP状态(在5.0.0中可用)

[英]Can't test HTTP status on MockMvc async REST service in spring-test 5.0.3 (works in 5.0.0)

我使用Spring Initializer( https://start.spring.io/ )Spring boot-2.0.0.RC1生成了一个新项目,该版本在5.0.3.RELEASE版本中提供了spring。

添加了以下控制器:

@RestController
@ResponseBody
@RequestMapping(value = "/customer")
public class CustomerController {
    @GetMapping("/")
    public Future<ResponseEntity<String>> get1() {
        return CompletableFuture.supplyAsync(() -> new ResponseEntity<String ("not found", HttpStatus.NOT_FOUND));
    }
}

并且在build.gradle中具有以下依赖项部分(问题也可以在Maven构建中复制)

dependencies {
    compile('org.springframework.boot:spring-boot-starter')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework:spring-web')
    testCompile('org.springframework:spring-test')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

以下测试失败:

@SpringBootTest
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
public class CustomerControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Test
    public void test404() throws Exception {
        MvcResult asyncResult = mockMvc.perform(MockMvcRequestBuilders.get("/customer/"))
                .andExpect(request().asyncStarted())
                .andReturn();

        mockMvc.perform(asyncDispatch(asyncResult))
                .andExpect(status().isNotFound());
    }
}

带有以下日志:

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /customer/
       Parameters = {}
          Headers = {}
             Body = null
    Session Attrs = {}

Handler:
             Type = com.example.wkicior.demo.CustomerController
           Method = public java.util.concurrent.Future<org.springframework.http.ResponseEntity<java.lang.String>> com.example.wkicior.demo.CustomerController.get1()

Async:
    Async started = true
     Async result = <404 Not Found,not found,{}>

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

MockHttpServletRequest:
      HTTP Method = GET
      Request URI = /customer/
       Parameters = {}
          Headers = {}
             Body = null
    Session Attrs = {}

Handler:
             Type = com.example.wkicior.demo.CustomerController
           Method = public java.util.concurrent.Future<org.springframework.http.ResponseEntity<java.lang.String>> com.example.wkicior.demo.CustomerController.get1()

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {Content-Type=[text/plain;charset=UTF-8], Content-Length=[9]}
     Content type = text/plain;charset=UTF-8
             Body = not found
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: Status 
Expected :404
Actual   :200

似乎测试将状态代码视为200,而不是404。不过,正文内容已正确返回。 有趣的是,降级到5.0.0。spring-test的发布-解决了这个问题:

testCompile('org.springframework:spring-test:5.0.0.RELEASE')

这是春天测试的错误吗?

看来确实像是弹簧虫: https ://jira.spring.io/browse/SPR-16430移至5.0.4.BUILD-SNAPSHOT解决了这个问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM