Skip to content

GoodforGod/nats-testcontainers

Repository files navigation

Nats TestContainers

Minimum required Java version Maven Central GitHub Action Quality Gate Status Coverage Maintainability Rating

This is Nats TestContainers module for running database as Docker container.

Dependency 🚀

Gradle

testImplementation "io.goodforgod:nats-testcontainers:0.1.0"

Maven

<dependency>
    <groupId>io.goodforgod</groupId>
    <artifactId>nats-testcontainers</artifactId>
    <version>0.1.0</version>
    <scope>test</scope>
</dependency>

Testcontainers

  • Version 0.1.0+ - build on top of Testcontainers 1.21.3

Usage

Check this TestContainers tutorials for Jupiter / JUnit 5 examples.

Run Nats container without authentication.

@Testcontainers
class NatsContainerTests {

    @Container
    private static final NatsContainer container = new NatsContainer("nats:2.11-alpine");

    @Test
    void checkContainerIsRunning() {
        assertTrue(container.isRunning());
    }
}

Container

Up & Running

Container implements startup strategy and will be available to TestContainer framework automatically when database will be ready for accepting connections.

Check here for more info about strategies.

Auth

All authentication options are available as per Nats Docker description.

No authorization is set by default in container.

Authentication Token

You can run Nats with authentication by token.

@Testcontainers
class NatsContainerTests {

    @Container
    private static final NatsContainer container = new NatsContainer()
            .withAuthTokenRandom();

    @Test
    void checkContainerIsRunning() {
        assertTrue(container.isRunning());
    }
}

You can also specify your own token:

@Testcontainers
class NatsContainerTests {

    @Container
    private static final NatsContainer container = new NatsContainer()
            .withAuthToken("myToken");

    @Test
    void checkContainerIsRunning() {
        assertTrue(container.isRunning());
    }
}

Authentication User Password

You can run container with authentication by username and password.

@Testcontainers
class NatsContainerTests {

    @Container
    private static final NatsContainer container = new NatsContainer()
            .withUsernameAndPassword("user", "pass");

    @Test
    void checkContainerIsRunning() {
        assertTrue(container.isRunning());
    }
}

Cluster

You can run NATS cluster as TestContainers.

Default cluster with 3 nodes is preconfigured for easy usage.

@Testcontainers
class NatsContainerTests {

    @Container
    private static final NatsCluster cluster = NatsCluster.builder("nats:2.11-alpine").build();

    @Test
    void checkClusterIsRunning() {
        assertTrue(cluster.isRunning());
    }
}

Cluster Builder

You can build cluster with desired size via NatsClusterBuilder.

You can check each container type via specified cluster container method.

final NatsCluster cluster = NatsCluster.builder("nats:2.11-alpine")
            .withNodes(5)              // 5 nodes
            .build();

License

This project licensed under the MIT - see the LICENSE file for details.

About

⚙️ TestContainers NATS module implementation.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages