Skip to content
/ Logsert Public

Logsert helps to test logging by recording log-events during test-runs and providing easy assertions.

License

Notifications You must be signed in to change notification settings

Nylle/Logsert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Logsert

Logsert helps to test logging functionality by recording log-events during test-runs and providing easy assertions.

Getting Started

<dependency>
    <groupId>com.github.nylle</groupId>
    <artifactId>logsert</artifactId>
    <version>2.2.0</version>
    <scope>test</scope>
</dependency>

Maven Central

Usage

LogAssertions

class SomethingThatLogsTest {

    @RegisterExtension
    LogRecorder logRecorder = new LogRecorder(SomethingThatLogs.class);

    @Test
    void fluentAssertionsAreConvenient() {
        var sut = new SomethingThatLogs();
        
        var expectedException = new RuntimeException("expected for test");

        sut.logInfoWithMdcAndException("message 1", Map.of("key", "value", "foo", "bar"), expectedException);
        sut.logInfo("message 2");
        sut.logInfo("message 3");

        LogAssertions.assertThat(logRecorder)
                .withMessage("message 1")
                .withLevel(Level.INFO)
                .withMdcEntry("foo", "bar")
                .withMdcEntry("key", "value")
                .withMdcEntries(Map.of("key", "value"))
                .withMdcEntries(Map.of("foo", "bar"))
                .withMdcEntriesExactly(Map.of("key", "value", "foo", "bar"))
                .withException(expectedException)
                .withException(RuntimeException.class)
                .withException(RuntimeException.class, "expected for test")
                .containsLogs(1)
                .withMessageContaing("message")
                .withLevel(Level.INFO)
                .containsLogs(3)
                .withMessage("foo")
                .containsLogs(0);
    }

    @Test
    void standardListAssertionsAreAlsoSupported() {
        var sut = new SomethingThatLogs();
        sut.logInfoWithMdcAndException("message", Map.of("key", "value"), new RuntimeException("expected for test"));

        Assertions.assertThat(logRecorder.getLogEvents())
                .extracting("level", "message", "MDCPropertyMap", "throwableProxy.className", "throwableProxy.message")
                .contains(tuple(Level.INFO, "message", Map.of("key", "value"), RuntimeException.class.getName(), "expected for test"));
    }
}

MeterAssertions

class MeterRegistryTest {

    MeterRegistry meterRegistry = new SimpleMeterRegistry();

    @Test
    void fluentAssertionsAreConvenient() {
        meterRegistry.gauge("gauge", Tags.of(Tag.of("key1", "value1")), 12);
        
        var counter = meterRegistry.counter("counter", Tags.of(Tag.of("key1", "value1")));
        counter.increment();
        counter.increment();
        
        var timer = meterRegistry.timer("timer", Tags.of(Tag.of("key1", "value1")));
        timer.record(60, TimeUnit.SECONDS);
        timer.record(180, TimeUnit.SECONDS);

        MeterAssertions.assertThat(meterRegistry)
                .withName("counter")
                .withTag("key1", "value1")
                .containsCount(2.0)
                .withName("counter")
                .withTag("key1", "value1")
                .ofType(Counter.class)
                .containsMeasurement()
                .withName("gauge")
                .withTag("key1", "value1")
                .containsGauge(12)
                .withName("timer")
                .withTag("key1", "value1")
                .containsTimer(2.0, 240.0, 180.0)
                .withName("missing")
                .containsNoMeasurements();
    }
}

About

Logsert helps to test logging by recording log-events during test-runs and providing easy assertions.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages