1. Unit test for logging

This chapter describes how to test if a specific line of code has been tested.

1.1. Unit test for Java Util Logging (JUL)

Here is a simple class we want to test :

public class ExampleJavaUtilLogging {

    public static final String HELLO = "Hello Turbo!";

    private static Logger log = Logger.getLogger(ExampleJavaUtilLogging.class.getName());

    public void logHello() {
        log.info( HELLO );
    }

}

Here is the full ExampleJavaUtilLogging.java java code.

And we want to test if the line "Hello Turbo!" has been actually logged :

@Test
void testLogHello() {
    ExampleJavaUtilLogging log = new ExampleJavaUtilLogging();
    log.logHello();
    Assertions.assertTrue( LOG_MESSAGE_INTERCEPTOR_HANDLER.containsLogMessage(ExampleJavaUtilLogging.HELLO) );
}

This is achieved using an implementation of java.util.loggin.Handler :

public static class LogMessageInterceptorHandler extends Handler {

    private Set<String> messages = new HashSet<>();

    @Override
    public void publish(LogRecord lr) {
        // add log messages to a set
        this.messages.add(lr.getMessage());
    }

    @Override
    public boolean isLoggable(LogRecord lr) {
        return super.isLoggable(lr);
    }

    public boolean containsLogMessage(String message) {
        // check if a message has been logged
        return this.messages.contains(message);
    }

}

Here is the full TestExampleJavaUtilLogging.java sample code.

I used this Unit Test the first time on a request for a PR opened on SnakeYAML project, here is the full code OptionToLogDuplicateKeysTest.java.

2. Unit test for exception

This chapter describes how to test when an exception is thrown.

2.1. Unit test for Exception when reading an InputStream

Here is a simple class we want to test :

public class ExampleReadStream {

    public byte[] readBytes( InputStream inputStream ) {
        try ( ByteArrayOutputStream buffer = new ByteArrayOutputStream() ) {
            byte[] data = new byte[1024];
            int read = inputStream.read( data );
            while ( read > 0 ) {
                buffer.write( data, 0, read );
                read = inputStream.read( data );
            }
            return buffer.toByteArray();
        } catch ( IOException e ) {
            String message = String.format( "Error reading input stream : %s", e );
            throw new ExampleRuntimeException( message, e );
        }
    }

}

Here is the full ExampleReadStream.java java code.

And to test if the exception is correctly handled, we can create a custom InputStream :

    @Test
    void testKo() throws IOException {
        ExampleReadStream example = new ExampleReadStream();
        try (InputStream is = new InputStream() {
            @Override
            public int read() throws IOException {
                if (Boolean.TRUE) {
                    throw new IOException("read failed (by scenario)");
                }
                return 0;
            }
        }) {
            Assertions.assertThrows(ExampleRuntimeException.class, () -> example.readBytes(is));
        }
    }

Here is the full TestExampleReadStream.java sample code.

I used this method the first time in the repository TestConfigStore.java.