我正在开发一个与Spring Boot(v2 M3)和Spring WebFlux相关的项目。在编码过程中,我发现我的微服务在返回一个flux时会返回一个 Http 200状态码,而实际上出现了错误。
因此,我进行了简单的测试: 我创建了一个简单的控制器。
@RestController
@RequestMapping(value = "/test")
public class TestController {
@GetMapping(value = "/errors")
public Flux<Object> getErrors() {
return Flux.error(new RuntimeException("test"));
}
}
通过一个简单的异常处理程序:
@ControllerAdvice
public class TestExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(TestExceptionHandler.class);
@ExceptionHandler(value = { RuntimeException.class })
public ResponseEntity<String> handleServerError(final RuntimeException e) {
LOGGER.error(e.getMessage(), e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
}
}
我使用webTestClient在集成测试中测试结果:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource("classpath:test.properties")
public class TestErrorsIT {
@Autowired
private WebTestClient webTestClient;
@Test
public void getErrors() {
this.webTestClient.get().uri("/test/errors").accept(MediaType.APPLICATION_STREAM_JSON).exchange().expectStatus()
.is5xxServerError();
}
}
因此,我的测试失败了,因为返回错误流的控制器返回的是200状态码而不是503状态码(异常日志“test”在我的控制台中被很好地跟踪)。你知道为什么吗?
java.lang.AssertionError: 响应状态值范围为200 预期值:但实际却是:
GET http://localhost:54432/test/errors WebTestClient-Request-Id: [1] Accept: [application/stream+json]
无内容
< 200 OK < Content-Type: [application/stream+json] < Transfer-Encoding: [chunked] < Date: [Fri, 03 Nov 2017 09:42:53 GMT]
内容暂时不可用