A Testcontainers module for NATS messaging system.
- Easy NATS container setup for integration testing
- JetStream support
- Authentication support (username/password)
- Convenient access to connection URLs and ports
- Debug and protocol tracing options
Add the dependency to your pom.xml:
<dependency>
<groupId>io.github.amadeusitgroup.testcontainers</groupId>
<artifactId>nats</artifactId>
<version>{version}</version>
<scope>test</scope>
</dependency>See latest version at Releases page.
try (NatsContainer nats = new NatsContainer("nats:2.12.1")) {
nats.start();
Options options = new Options.Builder()
.server(nats.getConnectionUrl())
.build();
try (Connection nc = Nats.connect(options)) {
// Use the NATS connection
}
}try (NatsContainer nats = new NatsContainer("nats:2.12.1").withJetStream()) {
nats.start();
Options options = new Options.Builder()
.server(nats.getConnectionUrl())
.build();
try (Connection nc = Nats.connect(options)) {
JetStream js = nc.jetStream();
// Use JetStream
}
}try (NatsContainer nats = new NatsContainer("nats:2.12.1")
.withAuth("username", "password")) {
nats.start();
Options options = new Options.Builder()
.server(nats.getConnectionUrl())
.userInfo("username", "password")
.build();
try (Connection nc = Nats.connect(options)) {
// Use authenticated connection
}
}| Method | Container Port | Description |
|---|---|---|
getClientPort() |
4222 | Returns the mapped host port for NATS client connections |
getRoutingPort() |
6222 | Returns the mapped host port for cluster/route connections |
getHttpMonitoringPort() |
8222 | Returns the mapped host port for HTTP monitoring |
| Method | Description |
|---|---|
getConnectionUrl() |
Returns nats://host:port for client connections |
getHttpMonitoringUrl() |
Returns http://host:port for monitoring endpoint |
| Method | Description |
|---|---|
withJetStream() |
Enables JetStream persistence |
withAuth(username, password) |
Configures authentication |
withDebug() |
Enables debug logging |
withProtocolTracing() |
Enables protocol tracing |
| Method | Returns | Description |
|---|---|---|
isJetStreamEnabled() |
boolean |
Whether JetStream was enabled via withJetStream() |
isDebugEnabled() |
boolean |
Whether debug logging was enabled via withDebug() |
isProtocolTracingEnabled() |
boolean |
Whether protocol tracing was enabled via withProtocolTracing() |
getUsername() |
String |
Username configured via withAuth(), or null |
getPassword() |
String |
Password configured via withAuth(), or null |
mvn clean installmvn testNote: Tests require Docker / Podman to be available.
Please read CONTRIBUTING.md for details on our Code of Conduct, and the process for submitting pull requests to us.
Apache License 2.0