Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 使用@WebFluxTest测试RouterFunction_Spring_Spring Boot_Testing_Spock_Spring Webflux - Fatal编程技术网

Spring 使用@WebFluxTest测试RouterFunction

Spring 使用@WebFluxTest测试RouterFunction,spring,spring-boot,testing,spock,spring-webflux,Spring,Spring Boot,Testing,Spock,Spring Webflux,我有一个路由器功能,我想用spock测试它。看起来像这样 @Configuration public class WebConfig { /** * Router function. * @return string */ @Bean public RouterFunction<?> helloRoute() { return route(GET("/judge/router/hello"),

我有一个路由器功能,我想用
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
对basic
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")
}
@RestController
@请求映射(“/judge/rest”)
公共类基本控制器{
私有静态最终记录器Logger=LoggerFactory.getLogger(BasicController.class);
@GetMapping(“/hello”)
公共单句柄(){
debug(“调用hello控制器”);
返回Mono.just(“hellowebflux”);
}
}
def“mvc mono返回hello”(){
期望:
webClient.get().uri(“/judge/rest/hello”)
.exchange()
.expectStatus().isOk()
.expectBody(String.class)
.isEqualTo(“你好,WebFlux”)
}

路由器功能失败的原因?

这是已知的
@WebFluxTest
限制-目前没有一致的方法来检测
路由器功能
bean,就像我们对
@Controller
类所做的那样

请参阅。

要添加内容,kizux在他发布的Github问题链接中似乎提到了一个解决方法

您可以使用
bindToApplicationContext
使用WebTestClient测试路由器功能

引用

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {RouteConfiguration.class, UserHandler.class})
@WebFluxTest
public class UserHandlerTest
{
    @Autowired
    private ApplicationContext context;
    @MockBean(name="userService")
    private UserService userService;
    private WebTestClient testClient;

    @Before
    public void setUp()
    {
        testClient = WebTestClient.bindToApplicationContext(context).build();
    }

    ...

所以没有办法测试它们?没有直接方法调用或一些嵌入式服务器?目前还没有
@WebFluxTest
等价物-因此,是的,嵌入式服务器或直接测试它们是一种方式。随着嵌入式数据库的兴起,是时候对嵌入式服务器进行测试了