簡體   English   中英

無法驗證模擬輸出流中的寫入值

[英]Cannot verify the written value in a mocked output stream

我的TcpTransport類中具有以下方法:

public synchronized void send(Envelope envelope) throws IOException {
    ensureSocketOpen();
    String envelopeString = envelopeSerializer.serialize(envelope);

    if (traceWriter != null &&
            traceWriter.isEnabled()) {
        traceWriter.trace(envelopeString, TraceWriter.DataOperation.SEND);
    }

    try {
        byte[] envelopeBytes = envelopeString.getBytes("UTF-8");
        outputStream.write(envelopeBytes);
        outputStream.flush();
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException("Could not convert the serialized envelope to a UTF-8 byte array", e);
    }
}

並對其進行測試:

// Arrange
TcpTransport target = getAndOpenTarget(); // gets a valid instance
Envelope envelope = mock(Envelope.class);
String serializedEnvelope = DataUtil.createRandomString(200);
when(envelopeSerializer.serialize(envelope)).thenReturn(serializedEnvelope);

// Act
target.send(envelope);

// Assert
verify(outputStream, times(1)).write(AdditionalMatchers.aryEq(serializedEnvelope.getBytes("UTF-8")));
verify(outputStream, atLeastOnce()).flush();

outputStream的實例是這樣創建的:

outputStream = mock(OutputStream.class);

我正在使用相同的方法將字符串轉換為UTF-8字節數組(這是同一實例,因為模擬的信封信封序列將其返回)。 但我收到以下錯誤:

Argument(s) are different! Wanted:
outputStream.write(
    [102, 100, 104, 54, 111, 104, 55, 99, 51, 101, 120, 119, 99, 114, 50,     120, 104, 98, 98, 51, 99, 48, 120, 106, 55, 114, 102, 114, 102, 103, 50, 111, 108, 122, 109, 119, 117, 100, 50, 102, 114, 112, 51, 109, 114, 121, 49, 114, 104, 104, 103, 101, 104, 112, 54, 48, 54, 122, 98, 101, 113, 100, 54, 116, 51, 102, 106, 56, 118, 99, 102, 52, 98, 98, 105, 114, 111, 112, 111, 52, 102, 56, 97, 107, 51, 98, 107, 105, 119, 49, 117, 97, 98, 99, 48, 55, 53, 121, 102, 101, 50, 105, 117, 110, 115, 112, 112, 102, 53, 100, 54, 120, 117, 115, 117, 105, 99, 105, 109, 111, 100, 111, 122, 101, 57, 116, 102, 110, 55, 114, 52, 97, 114, 55, 48, 49, 113, 51, 97, 116, 57, 115, 110, 112, 55, 52, 106, 111, 101, 121, 112, 54, 100, 112, 111, 56, 102, 52, 115, 51, 99, 98, 99, 98, 102, 118, 107, 57, 99, 48, 98, 114, 111, 111, 115, 111, 120, 119, 106, 101, 103, 102, 99, 116, 98, 98, 114, 109, 110, 116, 113, 97, 53, 120, 52, 113, 111, 114, 118, 110]
);
-> at     org.limeprotocol.network.tcp.TcpTransportTest.send_validArgumentsAndOpenStreamAndTraceEnabled_callsWriteAndTraces(TcpTransportTest.java:98)
Actual invocation has different arguments:
outputStream.write(
    [102, 100, 104, 54, 111, 104, 55, 99, 51, 101, 120, 119, 99, 114, 50, 120, 104, 98, 98, 51, 99, 48, 120, 106, 55, 114, 102, 114, 102, 103, 50, 111, 108, 122, 109, 119, 117, 100, 50, 102, 114, 112, 51, 109, 114, 121, 49, 114, 104, 104, 103, 101, 104, 112, 54, 48, 54, 122, 98, 101, 113, 100, 54, 116, 51, 102, 106, 56, 118, 99, 102, 52, 98, 98, 105, 114, 111, 112, 111, 52, 102, 56, 97, 107, 51, 98, 107, 105, 119, 49, 117, 97, 98, 99, 48, 55, 53, 121, 102, 101, 50, 105, 117, 110, 115, 112, 112, 102, 53, 100, 54, 120, 117, 115, 117, 105, 99, 105, 109, 111, 100, 111, 122, 101, 57, 116, 102, 110, 55, 114, 52, 97, 114, 55, 48, 49, 113, 51, 97, 116, 57, 115, 110, 112, 55, 52, 106, 111, 101, 121, 112, 54, 100, 112, 111, 56, 102, 52, 115, 51, 99, 98, 99, 98, 102, 118, 107, 57, 99, 48, 98, 114, 111, 111, 115, 111, 120, 119, 106, 101, 103, 102, 99, 116, 98, 98, 114, 109, 110, 116, 113, 97, 53, 120, 52, 113, 111, 114, 118, 110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  
(+ a lot of zeros)
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
0,
200
);

好像模擬的outputStream實例的verify方法正在對照內部緩沖區檢查參數的值,該緩沖區的長度為8192。 模擬的OutputStream的生成類為OutputStream $$ EnhancerByMockitoWithCGLIB $$ b1c65902

有什么想法可以驗證Mockito是否已使用正確的值調用write方法嗎?

與其模擬輸出流,不如提供一個。 特別是對於文字。

outputStream = new ByteArrayOutputStream();
outputStream.write("Hello World".getBytes());

assertEquals("Hello World", outputStream.toString());

如果要檢查flush(),可以這樣做

boolean[] flushed = { false };
outputStream = new ByteArrayOutputStream() {
    public void flush() throws IOException {
        super.flush();
        flushed[0] = true;
    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM