Junittest上传java webflux服务

qgelzfjb  于 2023-01-01  发布在  Java
关注(0)|答案(1)|浏览(81)

晚安
当我尝试执行此测试时,返回此错误。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);}

类服务:

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()的方法吗?

t2a7ltrp

t2a7ltrp1#

试试这个。

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,用于测试React流。尝试使用该框架中的StepVerifier
您可以找到一些示例here

相关问题