繁体   English   中英

junittest上传java webflux服务

[英]Junittest upload java webflux service

晚安:

当我尝试执行此测试时,返回此错误。 java.lang.NullPointerException: at the height of.map(dataBuffer -> { lines below....

附测试:

 @Test
    void testing_1() throws UnsupportedEncodingException {
        FilePart filePart = BDDMockito.mock(FilePart.class);
        BDDMockito.given(filePart.filename()).willReturn("resources/ejemplo.csv");
        Mockito.when(repo.metodo(any())).thenReturn("Abcd");                
        service.importData(filePart);}

Class服务:

public Flux<String> importData(FilePart filePart) {
        return filePart.content().map(dataBuffer -> { // This line throw NullPointerException
                    byte[] bytes = new byte[dataBuffer.readableByteCount()];
                    dataBuffer.read(bytes);
                    DataBufferUtils.release(dataBuffer);
                    return bytes;})
                .map(t -> {});
}

也许您知道模拟 filePart 或 filePart.content() 的方法吗?

试试这个。

    byte [] dataBufferBytes =  // define your test byte array data here. 
    DataBuffer dataBuffer = Mockito.mock(DataBuffer.class);
    doReturn(dataBuffer).when(filePart).content();
    doReturn(dataBufferBytes).when(dataBuffer).readableByteCount();

除此之外,我认为这个测试不会在 WebFlux 中以这种方式执行。 有一个不同的测试框架用于测试反应流,称为reactor-test 尝试使用该框架中的StepVerifier

你可以在这里找到一些例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM