2

I have implemented following Chronicle Queue and for this I want to write unit test case. How do I capture ArgumentCaptor arguments while using lambda. I want to get writeBuffer in test case to verify the data.

public class Persister  {

public Persister(SingleChronicleQueue chronicle) {
    this.chronicle = chronicle;
}
public void write(int outBufferOffset) throws IOException {
       ExcerptAppender appender = chronicle.acquireAppender();
       appender.writeBytes(b -> {
               b.writeInt(outBufferOffset);                    
               b.write(writeBuffer, 0 , outBufferOffset);
       });}
}

Test case:

@Mock
SingleChronicleQueue chronicle;
@Mock
ExcerptAppender appender;
@Captor
ArgumentCaptor<WriteBytesMarshallable> argumentCaptorLambda;

Persister persister = new Persister();
@Test
public void shouldPersistByteMessage() throws IOException {
    persister.write(MESSAGEBYTES);

    verify(appender).writeBytes(argumentCaptorLambda.capture());
    WriteBytesMarshallable lastValue =  argumentCaptorLambda.getValue();

    //final byte[] persistedBytes = ?? how to get writeBuffer here ??
    //assertThat(readPersistedMessage(persistedBytes), is(MESSAGE));
}

Thanks

AmbGup
  • 731
  • 1
  • 9
  • 23

1 Answers1

4

I have used the doAnswer for this implementation

@Captor
ArgumentCaptor<WriteBytesMarshallable> argumentCaptorLambda;
@Mock private BytesOut bytesOut;


public void shouldPersistReliableMessage() throws IOException {
        doAnswer(invocationOnMock -> {
            WriteBytesMarshallable marshallable = (WriteBytesMarshallable) invocationOnMock.getArguments()[0];
            marshallable.writeMarshallable(bytesOut);
            return null;
        }).when(appender).writeBytes((WriteBytesMarshallable)any());

        persister.persist(MESSAGEBYTES);

        verify(bytesOut).writeInt(84);
        verify(bytesOut).write(argumentCaptor.capture(), eq(0), eq(84));

        assertThat(readPersistedMessage(argumentCaptor.getValue()), is(MESSAGE));

    }
AmbGup
  • 731
  • 1
  • 9
  • 23