我有一个路由器功能,我想使用 spock
进行测试。看起来像这样
@Configuration
public class WebConfig {
/**
* Router function.
* @return string
*/
@Bean
public RouterFunction<?> helloRoute() {
return route(GET("/judge/router/hello"),
request -> ServerResponse.ok().body(fromPublisher(Mono.just("Hello Router WebFlux"), String.class)));
}
}
它的测试看起来像这样
@WebFluxTest
class JudgeRuleEngineMvcTestSpec extends Specification {
@Autowired
WebTestClient webClient;
def "router function returns hello"() {
expect:
webClient.get().uri("/judge/router/hello")
.exchange()
.expectStatus().isOk()
.expectBody(String.class)
.isEqualTo("Hello WebFlux") // should fail
}
}
但它失败了,因为它返回的不是 200
状态,而是 404
。它似乎找不到 REST
本身。
我还使用 GetMapping
对基本 RestController
进行了测试,它工作正常。
@RestController
@RequestMapping("/judge/rest")
public class BasicController {
private static final Logger LOGGER = LoggerFactory.getLogger(BasicController.class);
@GetMapping("/hello")
public Mono<String> handle() {
LOGGER.debug("Invoking hello controller");
return Mono.just("Hello WebFlux");
}
}
def "mvc mono returns hello"() {
expect:
webClient.get().uri("/judge/rest/hello")
.exchange()
.expectStatus().isOk()
.expectBody(String.class)
.isEqualTo("Hello WebFlux")
}
为什么路由器功能失败了?
最佳答案
这是 @WebFluxTest
的一个已知限制 - 目前没有一致的方法来检测 RouterFunction
bean,就像我们为 @Controller
类(class)。
关于spring - 使用@WebFluxTest 测试 RouterFunction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48842652/