This project provides ability to run pieces of Java code inside 🐳Docker container and access objects like they’re outside.

This creates infinite possibilities for integration testing.

1. Installation

Maven

Add this to your pom.xml:

<dependency>
    <groupId>com.github.librudder</groupId>
    <artifactId>rudder</artifactId>
    <version>1.0-SNAPSHOT</version>
</dependency>

Gradle

Add this to your build.gradle:

implementation 'com.github.librudder:rudder:1.0-SNAPSHOT'

2. Usage

Inherit from application you want to test. This application must have public static void main method and must implement RudderApplication<T> where T is application class. Also there must be a static setReadyCallback(Consumer<T>)

Example class to test:

import java.util.function.Consumer;
class TestApplication {

    public static void main(String[] args){

    }

    public String getCurrentHostname() {
        return System.getenv("HOSTNAME");
    }

}

class TestRudderApplication implements RudderApplication<TestRudderApplication> {

    private static Consumer<TestRudderApplication> callback;

    public static void setReadyCallback(final Consumer<TestRudderApplication> callback) {
        TestRudderApplication.callback = callback;
    }

    public static void main(String[] args){
        TestApplication.main(args);
        callback.apply(new TestRudderApplicatiion());
    }

}

And our test starter:

class Tester {
    public static void main(String[] args){
        Class<?> clazz = TestApplication.class;
        ContaineredApplication<TestApplication> container = new ContaineredApplication<>("SomeName", "adoptopenjdk/openjdk11:x86_64-ubuntu-jdk-11.28", clazz, List.of("raz", "dva"));
        container.start();

        final TestApplication application = container.getApplication();
        System.out.println(application.getCurrentHostname());
    }
}

This example will print the HOSTNAME environment variable defined inside the container (usually it’s container ID).

For more real life example check Spring example.