我已经实现了以下Chronicle Queue,为此我想编写单元测试用例。如何在使用lambda时捕获ArgumentCaptor参数。我想在测试用例中使用writeBuffer来验证数据。
# cat logstash-syslog2.conf
input {
file {
path => [ "/scratch/rsyslog/*/messages.log" ]
type => "syslog"
}
}
filter {
if [type] == "syslog" {
grok {
match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:hostname} %{DATA:program}(?:\[%{POSINT:pid}\])?: %{GREEDYDATA:syslog_message}" }
}
date {
match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
}
mutate {
remove_field => [
"message",
"pid",
"port"
"_grokparsefailure"
]
}
mutate {
replace => [
"@source_host", "%{allLogs_hostname}"
"@message", "%{allLogs_message}"
]
}
mutate {
remove => [
"allLogs_hostname",
"syslog_message",
"syslog_timestamp"
]
}
}
output {
if [type] == "syslog" {
elasticsearch {
hosts => "localhost:9200"
index => "%{type}-%{+YYYY.MM.dd}"
}
}
}
测试用例:
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);
});}
}
由于
答案 0 :(得分:2)
我已将doAnswer用于此实现
@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));
}