簡體   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