Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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
Java 具有区间响应的Spring-WebFlux控制器_Java_Spring_Spring Webflux_Junit5_Web Client - Fatal编程技术网

Java 具有区间响应的Spring-WebFlux控制器

Java 具有区间响应的Spring-WebFlux控制器,java,spring,spring-webflux,junit5,web-client,Java,Spring,Spring Webflux,Junit5,Web Client,我很难为webflux控制器创建一个有效的测试,该控制器每隔一秒返回一个通量。在我的多次尝试中,我可能已经接近了好几次,但总是有一些地方不对劲,我一直未能找到解决办法 控制员: @RestController public class SWFluxDemoController { @Autowired SWFluxDemoService swFluxDemoService; // Messages are Sent to the client as Server Se

我很难为webflux控制器创建一个有效的测试,该控制器每隔一秒返回一个通量。在我的多次尝试中,我可能已经接近了好几次,但总是有一些地方不对劲,我一直未能找到解决办法

控制员:

@RestController
public class SWFluxDemoController {

    @Autowired
    SWFluxDemoService swFluxDemoService;

    // Messages are Sent to the client as Server Sent Events
    @GetMapping(value = "/", produces = MediaType.TEXT_HTML_VALUE)
    public Flux<String> pushEventSignal() {
        return swFluxDemoService.getInfinityString();
    }

}
这就是我想要的

问题是在Spring工具套件中编写和执行测试。我想做一些类似于下面的事情,但这不会通过语法检查,更不用说运行了

@RunWith(SpringRunner.class)
@ContextConfiguration(classes={SWFluxDemoController.class,SWFluxDemoService.class})
@WebFluxTest(controllers=SWFluxDemoController.class)
public class SimpleWebFluxDemoApplicationTests {


    @Autowired
    private WebTestClient webClient;

    @Test
    public void get() throws Exception{

        FluxExchangeResult<String> result = webClient.get().uri("/").accept(MediaType.TEXT_EVENT_STREAM)
        .exchange()
 //       .expectStatus().isCreated()
        .returnResult(String.class);        


        StepVerifier.withVirtualTime({ result })
                .expectSubscription()
                .thenAwait(Duration.ofSeconds(1))
                .expectNext(0);

    }
}
@RunWith(SpringRunner.class)
@ContextConfiguration(类={SWFluxDemoController.class,SWFluxDemoService.class})
@WebFluxTest(控制器=SWFluxDemoController.class)
公共类SimpleWebFluxDemoApplicationTests{
@自动连线
私有WebTestClient网络客户端;
@试验
public void get()引发异常{
FluxExchangeResult=webClient.get().uri(“/”).accept(MediaType.TEXT\u事件\u流)
.exchange()
//.expectStatus().isCreated()
.returnResult(String.class);
StepVerifier.withVirtualTime({result})
.1.1.2(订阅)
.然后等待(持续时间秒(1))
.1下一步(0);
}
}
使用注释掉的StepVerifier代码对webclient调用的任何预期状态进行编码将返回一个错误,即无可用内容


非常感谢您的帮助。

您的控制器声明它生成
文本\u HTML\u值
;然而,您的测试声明它接受
TEXT\u EVENT\u STREAM

这就是导致406响应码的原因


因此,您需要确保生成和接受的媒体类型是兼容的。

您的控制器声明它生成
TEXT\u HTML\u值
;然而,您的测试声明它接受
TEXT\u EVENT\u STREAM

这就是导致406响应码的原因


因此,您需要确保生成和接受的媒体类型是兼容的。

经过仔细研究,我终于找到了解决方案。我的测试结果如下:

    @Test
    public void get() throws Exception{



 FluxExchangeResult<String> result = webClient.get().uri("/").accept(MediaType.TEXT_EVENT_STREAM)
        .exchange()
      .returnResult(String.class);

    Flux<String> intervalString = result.getResponseBody();

        StepVerifier.create(intervalString)        
                .expectSubscription()
                .thenAwait(Duration.ofSeconds(1))
                .expectNextCount(0)
        .thenAwait(Duration.ofSeconds(1))
        .expectNextCount(1)
        .thenAwait(Duration.ofSeconds(1))
        .expectNextCount(2);        
    }
@测试
public void get()引发异常{
FluxExchangeResult=webClient.get().uri(“/”).accept(MediaType.TEXT\u事件\u流)
.exchange()
.returnResult(String.class);
通量intervalString=result.getResponseBy();
StepVerifier.create(intervalString)
.1.1.2(订阅)
.然后等待(持续时间秒(1))
.expectNextCount(0)
.然后等待(持续时间秒(1))
.expectNextCount(1)
.然后等待(持续时间秒(1))
.expectNextCount(2);
}

经过周旋,我终于找到了解决办法。我的测试结果如下:

    @Test
    public void get() throws Exception{



 FluxExchangeResult<String> result = webClient.get().uri("/").accept(MediaType.TEXT_EVENT_STREAM)
        .exchange()
      .returnResult(String.class);

    Flux<String> intervalString = result.getResponseBody();

        StepVerifier.create(intervalString)        
                .expectSubscription()
                .thenAwait(Duration.ofSeconds(1))
                .expectNextCount(0)
        .thenAwait(Duration.ofSeconds(1))
        .expectNextCount(1)
        .thenAwait(Duration.ofSeconds(1))
        .expectNextCount(2);        
    }
@测试
public void get()引发异常{
FluxExchangeResult=webClient.get().uri(“/”).accept(MediaType.TEXT\u事件\u流)
.exchange()
.returnResult(String.class);
通量intervalString=result.getResponseBy();
StepVerifier.create(intervalString)
.1.1.2(订阅)
.然后等待(持续时间秒(1))
.expectNextCount(0)
.然后等待(持续时间秒(1))
.expectNextCount(1)
.然后等待(持续时间秒(1))
.expectNextCount(2);
}

单元测试中的响应类型是FluxExchangeResult,它需要文本\u事件\u流媒体类型,这是接收类查看端点的方式。。我从值TEXT_HTML_值开始,IDE中显示一个错误:WebTestClient.RequestHeadersSpec类型中的方法accept(MediaType…)不适用于参数(字符串)。工作单元测试已作为解决方案发布。OK。很高兴你解决了!单元测试中的响应类型是FluxExchangeResult,它需要文本\事件\流媒体类型,这是接收类查看端点的方式。。我从值TEXT_HTML_值开始,IDE中显示一个错误:WebTestClient.RequestHeadersSpec类型中的方法accept(MediaType…)不适用于参数(字符串)。工作单元测试已作为解决方案发布。OK。很高兴你解决了!
@RunWith(SpringRunner.class)
@ContextConfiguration(classes={SWFluxDemoController.class,SWFluxDemoService.class})
@WebFluxTest(controllers=SWFluxDemoController.class)
public class SimpleWebFluxDemoApplicationTests {


    @Autowired
    private WebTestClient webClient;

    @Test
    public void get() throws Exception{

        FluxExchangeResult<String> result = webClient.get().uri("/").accept(MediaType.TEXT_EVENT_STREAM)
        .exchange()
 //       .expectStatus().isCreated()
        .returnResult(String.class);        


        StepVerifier.withVirtualTime({ result })
                .expectSubscription()
                .thenAwait(Duration.ofSeconds(1))
                .expectNext(0);

    }
}
    @Test
    public void get() throws Exception{



 FluxExchangeResult<String> result = webClient.get().uri("/").accept(MediaType.TEXT_EVENT_STREAM)
        .exchange()
      .returnResult(String.class);

    Flux<String> intervalString = result.getResponseBody();

        StepVerifier.create(intervalString)        
                .expectSubscription()
                .thenAwait(Duration.ofSeconds(1))
                .expectNextCount(0)
        .thenAwait(Duration.ofSeconds(1))
        .expectNextCount(1)
        .thenAwait(Duration.ofSeconds(1))
        .expectNextCount(2);        
    }