Class LogAssertionsUtil
To use this utility, you should use the WithLogCapture annotation on your test class. This will set up log
capturing for your test and make the logs available for assertions.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRepresents a specification of an assertion for a log event.static enumRepresents a specification for an expected exception.static final recordRepresents a specification for an expected exception. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidassertLogged(nl.altindag.log.LogCaptor logCaptor, int position, String message) Ensures that a message was logged.static voidassertLogged(nl.altindag.log.LogCaptor logCaptor, int position, String message, LogAssertionsUtil.MessageComparisonMethod comparisonMethod) Ensures that a message was logged.static voidassertLogged(nl.altindag.log.LogCaptor logCaptor, String message) Ensures that a message was logged.static voidassertLogged(nl.altindag.log.LogCaptor logCaptor, String message, LogAssertionsUtil.MessageComparisonMethod comparisonMethod) Ensures that a message was logged.static voidassertLogged(LogAssertionsUtil.LogAssertion logAssertion) Asserts that a log event was logged.static voidassertThrowableLogged(nl.altindag.log.LogCaptor logCaptor, int position, @Nullable String expectedMessage, Class<?>... expectedTypes) Ensures that a throwable with the correct causes was logged.static voidassertThrowableLogged(nl.altindag.log.LogCaptor logCaptor, int position, @Nullable String expectedMessage, Class<?> throwableType0, @Nullable String throwableMessage0) Ensures that a throwable with the correct causes was logged.static voidassertThrowableLogged(nl.altindag.log.LogCaptor logCaptor, int position, @Nullable String expectedMessage, Class<?> throwableType0, @Nullable String throwableMessage0, Class<?> throwableType1, @Nullable String throwableMessage1) Ensures that a throwable with the correct causes was logged.static voidassertThrowableLogged(nl.altindag.log.LogCaptor logCaptor, int position, @Nullable String expectedMessage, Class<?> throwableType0, @Nullable String throwableMessage0, Class<?> throwableType1, @Nullable String throwableMessage1, Class<?> throwableType2, @Nullable String throwableMessage2) Ensures that a throwable with the correct causes was logged.static voidassertThrowableLogged(nl.altindag.log.LogCaptor logCaptor, int position, @Nullable String expectedMessage, Class<?> throwableType0, @Nullable String throwableMessage0, Class<?> throwableType1, @Nullable String throwableMessage1, Class<?> throwableType2, @Nullable String throwableMessage2, Class<?> throwableType3, @Nullable String throwableMessage3) Ensures that a throwable with the correct causes was logged.static voidassertThrowableLogged(nl.altindag.log.LogCaptor logCaptor, int position, @Nullable String expectedMessage, LogAssertionsUtil.ThrowableSpec... throwableSpecs) Ensures that a throwable with the correct causes was logged.static voidassertThrowableNesting(@Nullable Throwable base, Class<?>... expectedTypes) Ensures that a throwable has the correct causes.static voidassertThrowableNesting(@Nullable Throwable base, LogAssertionsUtil.ThrowableSpec... throwableSpecs) Ensures that a throwable has the correct causes.static voidassertThrowingCount(nl.altindag.log.LogCaptor logCaptor, int count) Ensures that a certain number of throwables were logged.static List<nl.altindag.log.model.LogEvent> getThrowingLogEvents(nl.altindag.log.LogCaptor logCaptor) Gets the number of log events that were logged with a throwable.logAssertionBuilder(nl.altindag.log.LogCaptor logCaptor) Creates a new instance of aLogAssertionsUtil.LogAssertion.LogAssertionBuilderwith the provided log captor.
-
Method Details
-
logAssertionBuilder
public static LogAssertionsUtil.LogAssertion.LogAssertionBuilder logAssertionBuilder(nl.altindag.log.LogCaptor logCaptor) Creates a new instance of aLogAssertionsUtil.LogAssertion.LogAssertionBuilderwith the provided log captor.- Parameters:
logCaptor- The log captor to use.- Returns:
- A new instance of a
LogAssertionsUtil.LogAssertion.LogAssertionBuilder.
-
assertLogged
Asserts that a log event was logged.Creates a new instance of a
LogAssertionsUtil.LogAssertion.LogAssertionBuilderusinglogAssertionBuilder(LogCaptor).- Parameters:
logAssertion- The log assertion to check.
-
assertLogged
public static void assertLogged(nl.altindag.log.LogCaptor logCaptor, int position, String message, LogAssertionsUtil.MessageComparisonMethod comparisonMethod) Ensures that a message was logged. The assertion will fail if the message was not logged.- Parameters:
logCaptor- The log captor to get the log event from.position- The position of the log event to check. 0 means the first (oldest) log event, 1 means the second, etc.If the position is negative, the position will be relative to the end of the log events. For example, -1 will get the last log event, -2 will get the second to last, etc.
message- The expected message that was logged.comparisonMethod- The method to use when comparing the expected and actual messages.
-
assertLogged
Ensures that a message was logged. The assertion will fail if the throwable was not logged.Shortcut for
assertLogged(LogCaptor, int, String, MessageComparisonMethod)with a position of -1 and usingLogAssertionsUtil.MessageComparisonMethod.EQUALSfor the comparison method.- Parameters:
logCaptor- The log captor to check.message- The expected message that was logged along with the throwable. If null, the message will not be checked.
-
assertLogged
Ensures that a message was logged. The assertion will fail if the message was not logged.Shortcut for
assertLogged(LogCaptor, int, String, MessageComparisonMethod)usingLogAssertionsUtil.MessageComparisonMethod.EQUALSfor the comparison method.- Parameters:
logCaptor- The log captor to check.position- The position of the log event to check. 0 means the first (oldest) log event, 1 means the second, etc.If the position is negative, the position will be relative to the end of the log events. For example, -1 will get the last log event, -2 will get the second to last, etc.
message- The expected message that was logged along with the throwable. If null, the message will not be checked.
-
assertLogged
public static void assertLogged(nl.altindag.log.LogCaptor logCaptor, String message, LogAssertionsUtil.MessageComparisonMethod comparisonMethod) Ensures that a message was logged. The assertion will fail if the message was not logged.Shortcut for
assertLogged(LogCaptor, int, String, MessageComparisonMethod)with an offset of 1.- Parameters:
logCaptor- The log captor to check.message- The expected message that was logged along with the throwable. If null, the message will not be checked.comparisonMethod- The method to use when comparing the expected and actual messages.
-
assertThrowingCount
public static void assertThrowingCount(nl.altindag.log.LogCaptor logCaptor, int count) Ensures that a certain number of throwables were logged. The assertion will fail if the number of throwables logged does not match the expected count.- Parameters:
logCaptor- The log captor to check.count- The expected number of throwables to be logged.
-
assertThrowableLogged
public static void assertThrowableLogged(nl.altindag.log.LogCaptor logCaptor, int position, @Nullable @Nullable String expectedMessage, Class<?>... expectedTypes) Ensures that a throwable with the correct causes was logged. The assertion will fail if the throwable was not logged.Shortcut for
assertThrowableNesting(Throwable, ThrowableSpec[]).- Parameters:
logCaptor- The log captor to check.position- The position of the log event to check. 0 means the first (oldest) log event, 1 means the second, etc.If the position is negative, the position will be relative to the end of the log events. For example, -1 will get the last log event, -2 will get the second to last, etc.
expectedMessage- The expected message that was logged along with the throwable. If null, the message will not be checked.expectedTypes- The expected composition of the throwable that was logged. SeeLogAssertionsUtil.ThrowableSpec.matches(Throwable).
-
assertThrowableLogged
public static void assertThrowableLogged(nl.altindag.log.LogCaptor logCaptor, int position, @Nullable @Nullable String expectedMessage, Class<?> throwableType0, @Nullable @Nullable String throwableMessage0) Ensures that a throwable with the correct causes was logged. The assertion will fail if the throwable was not logged.Shortcut for
assertThrowableNesting(Throwable, ThrowableSpec[])withThrowableSpec(Class, String).- Parameters:
logCaptor- The log captor to check.position- The position of the log event to check. 0 means the first (oldest) log event, 1 means the second, etc.If the position is negative, the position will be relative to the end of the log events. For example, -1 will get the last log event, -2 will get the second to last, etc.
expectedMessage- The expected message that was logged along with the throwable. If null, the message will not be checked.throwableType0- The expected type of the base throwable.throwableMessage0- The expected message of the base throwable.
-
assertThrowableLogged
public static void assertThrowableLogged(nl.altindag.log.LogCaptor logCaptor, int position, @Nullable @Nullable String expectedMessage, Class<?> throwableType0, @Nullable @Nullable String throwableMessage0, Class<?> throwableType1, @Nullable @Nullable String throwableMessage1) Ensures that a throwable with the correct causes was logged. The assertion will fail if the throwable was not logged.Shortcut for
assertThrowableNesting(Throwable, ThrowableSpec[])withThrowableSpec(Class, String).- Parameters:
logCaptor- The log captor to check.position- The position of the log event to check. 0 means the first (oldest) log event, 1 means the second, etc.If the position is negative, the position will be relative to the end of the log events. For example, -1 will get the last log event, -2 will get the second to last, etc.
expectedMessage- The expected message that was logged along with the throwable. If null, the message will not be checked.throwableType0- The expected type of the base throwable.throwableMessage0- The expected message of the base throwable.throwableType1- The expected type of the first nested throwable.throwableMessage1- The expected message of the first nested throwable.
-
assertThrowableLogged
public static void assertThrowableLogged(nl.altindag.log.LogCaptor logCaptor, int position, @Nullable @Nullable String expectedMessage, Class<?> throwableType0, @Nullable @Nullable String throwableMessage0, Class<?> throwableType1, @Nullable @Nullable String throwableMessage1, Class<?> throwableType2, @Nullable @Nullable String throwableMessage2) Ensures that a throwable with the correct causes was logged. The assertion will fail if the throwable was not logged.Shortcut for
assertThrowableNesting(Throwable, ThrowableSpec[])withThrowableSpec(Class, String).- Parameters:
logCaptor- The log captor to check.position- The position of the log event to check. 0 means the first (oldest) log event, 1 means the second, etc.If the position is negative, the position will be relative to the end of the log events. For example, -1 will get the last log event, -2 will get the second to last, etc.
expectedMessage- The expected message that was logged along with the throwable. If null, the message will not be checked.throwableType0- The expected type of the base throwable.throwableMessage0- The expected message of the base throwable.throwableType1- The expected type of the first nested throwable.throwableMessage1- The expected message of the first nested throwable.throwableType2- The expected type of the second nested throwable.throwableMessage2- The expected message of the second nested throwable.
-
assertThrowableLogged
public static void assertThrowableLogged(nl.altindag.log.LogCaptor logCaptor, int position, @Nullable @Nullable String expectedMessage, Class<?> throwableType0, @Nullable @Nullable String throwableMessage0, Class<?> throwableType1, @Nullable @Nullable String throwableMessage1, Class<?> throwableType2, @Nullable @Nullable String throwableMessage2, Class<?> throwableType3, @Nullable @Nullable String throwableMessage3) Ensures that a throwable with the correct causes was logged. The assertion will fail if the throwable was not logged.Shortcut for
assertThrowableNesting(Throwable, ThrowableSpec[])withThrowableSpec(Class, String).- Parameters:
logCaptor- The log captor to check.position- The position of the log event to check. 0 means the first (oldest) log event, 1 means the second, etc.If the position is negative, the position will be relative to the end of the log events. For example, -1 will get the last log event, -2 will get the second to last, etc.
expectedMessage- The expected message that was logged along with the throwable. If null, the message will not be checked.throwableType0- The expected type of the base throwable.throwableMessage0- The expected message of the base throwable.throwableType1- The expected type of the first nested throwable.throwableMessage1- The expected message of the first nested throwable.throwableType2- The expected type of the second nested throwable.throwableMessage2- The expected message of the second nested throwable.throwableType3- The expected type of the third nested throwable.throwableMessage3- The expected message of the third nested throwable.
-
assertThrowableLogged
public static void assertThrowableLogged(nl.altindag.log.LogCaptor logCaptor, int position, @Nullable @Nullable String expectedMessage, LogAssertionsUtil.ThrowableSpec... throwableSpecs) Ensures that a throwable with the correct causes was logged. The assertion will fail if the throwable was not logged.- Parameters:
logCaptor- The log captor to check.position- The position of the log event to check. 0 means the first (oldest) log event, 1 means the second, etc.If the position is negative, the position will be relative to the end of the log events. For example, -1 will get the last log event, -2 will get the second to last, etc.
expectedMessage- The expected message that was logged along with the throwable. If null, the message will not be checked.throwableSpecs- The expected composition of the throwable that was logged. SeeassertThrowableNesting(Throwable, ThrowableSpec[]).
-
assertThrowableNesting
public static void assertThrowableNesting(@Nullable @Nullable Throwable base, Class<?>... expectedTypes) Ensures that a throwable has the correct causes. The assertion will fail if the throwables do not match the expected nesting.Shortcut for
assertThrowableNesting(Throwable, ThrowableSpec[])withLogAssertionsUtil.ThrowableSpec.of(Class[]).None of the messages will be checked.
- Parameters:
base- The base throwable to check.expectedTypes- The expected composition of the throwable that was logged. SeeLogAssertionsUtil.ThrowableSpec.matches(Throwable).
-
assertThrowableNesting
public static void assertThrowableNesting(@Nullable @Nullable Throwable base, LogAssertionsUtil.ThrowableSpec... throwableSpecs) Ensures that a throwable has the correct causes. The assertion will fail if the throwables do not match the expected nesting.This method checks the nesting of the throwables, meaning that the first throwable in the array is expected to be the root cause of the second, which is expected to be the root cause of the third, and so on.
If the base throwable is null, the method will throw an
AssertionFailedError.For each throwable spec, the method will ensure that the throwable matches the provided spec. See
LogAssertionsUtil.ThrowableSpec.matches(Throwable).For example, when providing "[{RuntimeException.class, null}, IOException.class, "Error"]" the following exception would be required to have been logged: new RuntimeException(new IOException("Error"));
- Parameters:
base- The base throwable to check.throwableSpecs- The expected composition of the throwable that was logged.- Throws:
IllegalArgumentException- If no throwables are specified.org.opentest4j.AssertionFailedError- If the throwables do not match the expected nesting.
-
getThrowingLogEvents
public static List<nl.altindag.log.model.LogEvent> getThrowingLogEvents(nl.altindag.log.LogCaptor logCaptor) Gets the number of log events that were logged with a throwable.- Parameters:
logCaptor- The log captor to check.- Returns:
- The number of log events that were logged with a throwable.
-