【发布时间】:2018-02-17 15:01:43
【问题描述】:
我有一个路由器功能,我想使用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
}
}
但它失败了,因为它返回 404 而不是 200 状态。它似乎找不到REST 本身。
我还用GetMapping 对基本@987654327@ 进行了测试,它工作正常。
@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")
}
为什么路由器功能会失败?
【问题讨论】:
标签: spring spring-boot testing spock spring-webflux