google / google/flogger

Add samples to show using Flogger in JUnit

Open
#170 3 comments 1 reaction 0 assignees View on GitHub
P4 type=documentation
Dominant language
Java
Stars
1.5k
Forks
133
Avg merge
6m
Merged PRs (30d)
7

Description

As part of unit testing, it becomes important to test the Logged messages in case of exceptions or other such scenarios.

Is this the recommended way to test logging behaviour in tests?
```java
@Test
public void read_invalidPath_logsError() {
Handler mockHandler = Mockito.mock(Handler.class);
ArgumentCaptor logRecordCaptor = ArgumentCaptor.forClass(LogRecord.class);
//set mock handler with Argument Captor
Logger.getLogger(JsonMessageParser.class.getName()).addHandler(mockHandler);

JsonMessageParser.of(TEST_JSON).read("$.code2");

verify(mockHandler).publish(logRecordCaptor.capture());

assertThat(logRecordCaptor.getValue().getMessage()).contains("error reading [$.code2]");
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.