All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kiwiproject.test.logback.InMemoryAppenderAssertions Maven / Gradle / Ivy

There is a newer version: 3.7.0
Show newest version
package org.kiwiproject.test.logback;

import ch.qos.logback.classic.spi.ILoggingEvent;
import org.assertj.core.api.Assertions;

import java.util.List;

/**
 * Provides AssertJ assertions for {@link InMemoryAppender}.
 */
public class InMemoryAppenderAssertions {

    private final InMemoryAppender appender;

    private InMemoryAppenderAssertions(InMemoryAppender appender) {
        this.appender = appender;
    }

    /**
     * Begin assertions for an {@link InMemoryAppender}.
     *
     * @param appender the appender to assert against
     * @return a new InMemoryAppenderAssertions instance
     */
    public static InMemoryAppenderAssertions assertThat(InMemoryAppender appender) {
        return new InMemoryAppenderAssertions(appender);
    }

    /**
     * Begin assertions for an {@link InMemoryAppender}.
     * 

* This method is an alias for {@link #assertThat(InMemoryAppender)}. * It can be used to avoid conflicts with AssertJ's {@code assertThat} * so that both can be used with static imports in the same test. * * @param appender the appender to assert against * @return a new InMemoryAppenderAssertions instance */ public static InMemoryAppenderAssertions assertThatAppender(InMemoryAppender appender) { return assertThat(appender); } /** * Assert this appender has the expected number of logging events, and if the assertion succeeds, return a * list containing those events. * * @return a List containing the logging events */ public List hasNumberOfLoggingEventsAndGet(int expectedEventCount) { return hasNumberOfLoggingEvents(expectedEventCount).andGetOrderedEvents(); } /** * Assert this appender has the expected number of logging events, and if the assertion succeeds, return this * instance to continue chaining assertions. * * @return this instance */ public InMemoryAppenderAssertions hasNumberOfLoggingEvents(int expectedEventCount) { var events = appender.orderedEvents(); Assertions.assertThat(events).hasSize(expectedEventCount); return this; } /** * Assert this appender contains the given message at least once (but possibly more than once). * * @param message the exact message to find * @return this instance */ public InMemoryAppenderAssertions containsMessage(String message) { var messages = appender.orderedEvents().stream().map(ILoggingEvent::getMessage).toList(); Assertions.assertThat(messages).contains(message); return this; } /** * A terminal method if you want to get the actual logging events after performing assertions, for example, * to perform additional inspections. Does not perform any assertions. * * @return a List containing the logging events */ public List andGetOrderedEvents() { return appender.orderedEvents(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy