Skip to content

Instantly share code, notes, and snippets.

@Test
public void testInfo(TestInfo testInfo) {
assertAll(() -> throwerrors("Just throwing"),
() -> throwerrors("Some random"),
() -> throwerrors("errors"));
}
public void throwerrors(String message) {
throw new RuntimeException(message);
}
@wkorando
wkorando / DisplaySpringBootVersion
Created April 11, 2018 18:14
Example of exposing the version of spring boot being used via actuator info endpoint
@Component
public class SpringBootVersion implements InfoContributor {
@Override
public void contribute(Builder builder) {
try {
Manifest mf = new Manifest();
mf.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("META-INF/MANIFEST.MF"));
Attributes atts = mf.getMainAttributes();
<configuration>
<appender name="static-appender" class="com.bk.logging.StaticAppender" />
<root level="trace">
<appender-ref ref="static-appender" />
</root>
</configuration>
public class TestStaticAppender {
@BeforeEach
public void clearLoggingStatements() {
StaticAppender.clearEvents();
}
@Test
public void testAssertingLoggingStatementsA() {
LogProducingService service = new LogProducingService();
public class StaticAppender extends AppenderBase<ILoggingEvent> {
static List<ILoggingEvent> events = new ArrayList<>();
@Override
public void append(ILoggingEvent e) {
events.add(e);
}
public static List<ILoggingEvent> getEvents() {
return events;
public class LogProducingService {
private static final Logger LOGGER = LoggerFactory.getLogger(LogProducingService.class);
public void writeSomeLoggingStatements(String message) {
LOGGER.info("Let's assert some logs! " + message);
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<?> future = executor.submit(() -> LOGGER.info("This message is in a separate thread"));
do {
// wait for future to complete
public class TestThreadSafeAppender {
@BeforeEach
public void clearLoggingStatements() {
ThreadSafeAppender.clearEvents();
}
@Test
public void testAssertingLoggingStatementsA() {
LogProducingService service = new LogProducingService();
service.writeSomeLoggingStatements("A");
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<appender name="threadsafe-appender" class="com.bk.logging.ThreadSafeAppender" />
<root level="trace">
<appender-ref ref="threadsafe-appender" />
</root>
</configuration>
public class ThreadSafeAppender extends AppenderBase<ILoggingEvent> {
static ThreadLocal<List<ILoggingEvent>> threadLocal = new ThreadLocal<>();
@Override
public void append(ILoggingEvent e) {
List<ILoggingEvent> events = threadLocal.get();
if (events == null) {
events = new ArrayList<>();
threadLocal.set(events);
}