-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5291c9
commit 83a9af7
Showing
4 changed files
with
112 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
dependencies { | ||
implementation "org.logcapture:logcapture-core:1.2.2" | ||
implementation "org.logcapture:logcapture-junit4:1.2.2" | ||
implementation "junit:junit:4.12" | ||
} |
96 changes: 96 additions & 0 deletions
96
logcapture-example/src/test/java/org/logcapture/example/ExampleShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.logcapture.example; | ||
|
||
import org.logcapture.junit4.LogCaptureRule; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.MDC; | ||
import org.slf4j.MarkerFactory; | ||
|
||
import static ch.qos.logback.classic.Level.INFO; | ||
import static org.logcapture.assertion.ExpectedLoggedException.logException; | ||
import static org.logcapture.assertion.ExpectedLoggingMessage.aLog; | ||
import static org.logcapture.matcher.exception.ExceptionCauseMatcher.causeOf; | ||
import static org.logcapture.matcher.exception.ExceptionCauseMessageMatcher.whereCauseMessage; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.isA; | ||
|
||
public class ExampleShould { | ||
private final Logger log = LoggerFactory.getLogger(ExampleShould.class); | ||
|
||
@Rule | ||
public LogCaptureRule logCaptureRule = new LogCaptureRule(); | ||
|
||
private ServiceThatLogs underTest = new ServiceThatLogs(); | ||
|
||
@Test | ||
public void verify_captured_events() { | ||
underTest.methodThatLogsStuff(); | ||
logCaptureRule.logged(aLog().info() | ||
.withMessage("a message")); | ||
} | ||
|
||
@Test | ||
public void verify_captured_events_with_marker() { | ||
log.info(MarkerFactory.getMarker("a_marker"), "a message"); | ||
logCaptureRule.logged(aLog() | ||
.withLevel(equalTo(INFO)) | ||
.withMarker("a_marker") | ||
.withMessage(equalTo("a message"))); | ||
} | ||
|
||
@Test | ||
public void verify_captured_events_with_exception() { | ||
RuntimeException exception = new RuntimeException(); | ||
|
||
underTest.methodThatLogs(exception); | ||
logCaptureRule.logged(aLog() | ||
.havingException(logException() | ||
.withException(isA(RuntimeException.class)) | ||
)); | ||
} | ||
|
||
@Test | ||
public void verify_captured_events_with_exception_cause_message() { | ||
RuntimeException exception = new RuntimeException(new IllegalStateException("Some state is invalid")); | ||
underTest.methodThatLogs(exception); | ||
logCaptureRule.logged(aLog() | ||
.havingException(logException() | ||
.withException(whereCauseMessage(containsString("state is invalid"))) | ||
)); | ||
} | ||
|
||
@Test | ||
public void verify_captured_events_with_exception_cause() { | ||
RuntimeException exception = new RuntimeException(new IllegalStateException("Some state is invalid")); | ||
underTest.methodThatLogs(exception); | ||
logCaptureRule.logged(aLog() | ||
.havingException(logException() | ||
.withException(causeOf(IllegalStateException.class)) | ||
)); | ||
} | ||
|
||
@Test | ||
public void verify_mdc_keys() { | ||
MDC.put("aKey", "someValue"); | ||
|
||
underTest.methodThatLogsStuff(); | ||
logCaptureRule.logged(aLog().info() | ||
.withMdc("aKey", equalTo("someValue")) | ||
.withMessage("a message")); | ||
} | ||
|
||
class ServiceThatLogs { | ||
|
||
void methodThatLogsStuff() { | ||
log.info("a message"); | ||
log.info("another message"); | ||
} | ||
|
||
void methodThatLogs(Exception exception) { | ||
log.error("message", exception); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters