@Sl4j
public class Hello {
public void writeFile() {
log.info("create file success!")
}
}
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.slf4j.LoggerFactory;
import static org.assertj.core.api.Assertions.assertThat;
class HelloTest {
private final Hello hello = new Hello();
private ListAppender<ILoggingEvent> getListAppenderForClass(Class clazz) {
Logger logger = (Logger) LoggerFactory.getLogger(clazz);
ListAppender<ILoggingEvent> loggingEventListAppender = new ListAppender<>();
loggingEventListAppender.start();
logger.addAppender(loggingEventListAppender);
return loggingEventListAppender;
}
@Test
public void testWriteFile() {
ListAppender<ILoggingEvent> logEvent = getListAppenderForClass(Hello.class);
hello.writeFile();
assertThat(logEvent.get(0)).isEqualTo("create file success!");
}
}