Package org.sonar.api.utils.log
Class LogTesterJUnit5
- java.lang.Object
-
- org.sonar.api.utils.log.LogTesterJUnit5
-
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterTestExecutionCallback,org.junit.jupiter.api.extension.BeforeTestExecutionCallback,org.junit.jupiter.api.extension.Extension
public class LogTesterJUnit5 extends java.lang.Object implements org.junit.jupiter.api.extension.BeforeTestExecutionCallback, org.junit.jupiter.api.extension.AfterTestExecutionCallbackFor tests only
This JUnit 5 extension allows to configure and access logs in tests. By default trace level is enabled.
Warning - not compatible with parallel execution of tests in the same JVM fork.
Example:public class MyClass { private final Logger logger = Loggers.get("logger_name"); public void doSomething() { logger.info("foo"); } } class MyClassTests { @org.junit.jupiter.api.extension.RegisterExtension LogTesterJUnit5 logTester = new LogTesterJUnit5(); @org.junit.jupiter.api.Test public void test_log() { new MyClass().doSomething(); assertThat(logTester.logs()).containsOnly("foo"); } }- Since:
- 8.1
-
-
Constructor Summary
Constructors Constructor Description LogTesterJUnit5()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafter()voidafterTestExecution(org.junit.jupiter.api.extension.ExtensionContext context)protected voidbefore()voidbeforeTestExecution(org.junit.jupiter.api.extension.ExtensionContext context)Gclear()java.util.List<LogAndArguments>getLogs()Logs with arguments in chronological order (item at index 0 is the oldest one)java.util.List<LogAndArguments>getLogs(LoggerLevel level)Logs with arguments in chronological order (item at index 0 is the oldest one) for a given leveljava.util.List<java.lang.String>logs()Logs in chronological order (item at index 0 is the oldest one)java.util.List<java.lang.String>logs(LoggerLevel level)Logs in chronological order (item at index 0 is the oldest one) for a given levelGsetLevel(LoggerLevel level)Enable/disable debug logs.
-
-
-
Method Detail
-
beforeTestExecution
public void beforeTestExecution(org.junit.jupiter.api.extension.ExtensionContext context) throws java.lang.Exception- Specified by:
beforeTestExecutionin interfaceorg.junit.jupiter.api.extension.BeforeTestExecutionCallback- Throws:
java.lang.Exception
-
afterTestExecution
public void afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext context) throws java.lang.Exception- Specified by:
afterTestExecutionin interfaceorg.junit.jupiter.api.extension.AfterTestExecutionCallback- Throws:
java.lang.Exception
-
before
protected void before()
-
after
protected void after()
-
setLevel
public G setLevel(LoggerLevel level)
Enable/disable debug logs. Info, warn and error logs are always enabled. By default INFO logs are enabled when LogTester is started.
-
logs
public java.util.List<java.lang.String> logs()
Logs in chronological order (item at index 0 is the oldest one)
-
logs
public java.util.List<java.lang.String> logs(LoggerLevel level)
Logs in chronological order (item at index 0 is the oldest one) for a given level
-
getLogs
public java.util.List<LogAndArguments> getLogs()
Logs with arguments in chronological order (item at index 0 is the oldest one)
-
getLogs
public java.util.List<LogAndArguments> getLogs(LoggerLevel level)
Logs with arguments in chronological order (item at index 0 is the oldest one) for a given level
-
clear
public G clear()
-
-