This is Nats TestContainers module for running database as Docker container.
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>- Version 0.1.0+ - build on top of Testcontainers 1.21.3
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 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.
All authentication options are available as per Nats Docker description.
No authorization is set by default in container.
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());
}
}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());
}
}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());
}
}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();This project licensed under the MIT - see the LICENSE file for details.