parent
93074cdabc
commit
b3f1413445
|
@ -33,6 +33,7 @@
|
|||
<module>selenium-junit-testng</module>
|
||||
<module>spring-testing</module>
|
||||
<module>test-containers</module>
|
||||
<module>testing-assertions</module>
|
||||
<module>testng</module>
|
||||
<module>junit-5-basics</module>
|
||||
<module>easymock</module>
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>testing-assertions</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.15.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.junit.log;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class BusinessWorker {
|
||||
private static Logger LOGGER = LoggerFactory.getLogger(BusinessWorker.class);
|
||||
|
||||
public void generateLogs(String msg) {
|
||||
LOGGER.trace(msg);
|
||||
LOGGER.debug(msg);
|
||||
LOGGER.info(msg);
|
||||
LOGGER.warn(msg);
|
||||
LOGGER.error(msg);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||
<Pattern>
|
||||
%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
|
||||
</Pattern>
|
||||
</layout>
|
||||
</appender>
|
||||
<logger name="com.baeldung.junit.log" level="debug" additivity="false">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</logger>
|
||||
<root level="error">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.junit.log;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.LoggerContext;
|
||||
|
||||
public class BusinessWorkerUnitTest {
|
||||
private static MemoryAppender memoryAppender;
|
||||
private static final String LOGGER_NAME = "com.baeldung.junit.log";
|
||||
private static final String MSG = "This is a test message!!!";
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
Logger logger = (Logger) LoggerFactory.getLogger(LOGGER_NAME);
|
||||
memoryAppender = new MemoryAppender();
|
||||
memoryAppender.setContext((LoggerContext) LoggerFactory.getILoggerFactory());
|
||||
logger.setLevel(Level.DEBUG);
|
||||
logger.addAppender(memoryAppender);
|
||||
memoryAppender.start();
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
memoryAppender.reset();
|
||||
memoryAppender.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
BusinessWorker worker = new BusinessWorker();
|
||||
worker.generateLogs(MSG);
|
||||
|
||||
// I check that I only have 4 messages (all but trace)
|
||||
assertThat(memoryAppender.countEventsForLogger(LOGGER_NAME)).isEqualTo(4);
|
||||
// I look for a specific message at a specific level, and I only have 1
|
||||
assertThat(memoryAppender.search(MSG, Level.INFO).size()).isEqualTo(1);
|
||||
// I check that the entry that is not present is the trace level
|
||||
assertThat(memoryAppender.contains(MSG, Level.TRACE)).isFalse();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.junit.log;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import ch.qos.logback.classic.Level;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.read.ListAppender;
|
||||
|
||||
/**
|
||||
* In memory slf4j appender<br/>
|
||||
* Convenient appender to be able to check slf4j invocations
|
||||
*/
|
||||
public class MemoryAppender extends ListAppender<ILoggingEvent> {
|
||||
public void reset() {
|
||||
this.list.clear();
|
||||
}
|
||||
|
||||
public boolean contains(String string, Level level) {
|
||||
return this.list.stream()
|
||||
.anyMatch(event -> event.getMessage().toString().contains(string)
|
||||
&& event.getLevel().equals(level));
|
||||
}
|
||||
|
||||
public int countEventsForLogger(String loggerName) {
|
||||
return (int) this.list.stream()
|
||||
.filter(event -> event.getLoggerName().contains(loggerName)).count();
|
||||
}
|
||||
|
||||
public List<ILoggingEvent> search(String string) {
|
||||
return this.list.stream()
|
||||
.filter(event -> event.getMessage().toString().contains(string))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<ILoggingEvent> search(String string, Level level) {
|
||||
return this.list.stream()
|
||||
.filter(event -> event.getMessage().toString().contains(string)
|
||||
&& event.getLevel().equals(level))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return this.list.size();
|
||||
}
|
||||
|
||||
public List<ILoggingEvent> getLoggedEvents() {
|
||||
return Collections.unmodifiableList(this.list);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||
<Pattern>
|
||||
%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
|
||||
</Pattern>
|
||||
</layout>
|
||||
</appender>
|
||||
<root level="error">
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
</root>
|
||||
</configuration>
|
Loading…
Reference in New Issue