gpt4 book ai didi

java - 无法在 spring-test 5.0.3 中测试 MockMvc 异步 REST 服务上的 HTTP 状态(适用于 5.0.0)

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:44 24 4
gpt4 key购买 nike

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

添加了以下 Controller :

@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。不过,正文内容已正确返回。有趣的是,降级到 spring-test 的 5.0.0.RELEASE - 解决了问题:

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

这是一个 spring 测试错误吗?

最佳答案

看起来确实像 spring bug: https://jira.spring.io/browse/SPR-16430迁移到 5.0.4.BUILD-SNAPSHOT 可以解决问题

关于java - 无法在 spring-test 5.0.3 中测试 MockMvc 异步 REST 服务上的 HTTP 状态(适用于 5.0.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48769616/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com