Skip to content

Commit 03f8caa

Browse files
slu-itkiview
authored andcommitted
Add example for MongoDB containers (#1288)
1 parent 0a66f3c commit 03f8caa

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
repositories {
6+
jcenter()
7+
}
8+
9+
dependencies {
10+
testImplementation 'org.testcontainers:testcontainers'
11+
testImplementation 'junit:junit:4.12'
12+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.testcontainers.containers;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
6+
/**
7+
* This {@link Container} is based on the official MongoDb ({@code mongo}) image from
8+
* <a href="https://hub.docker.com/r/_/mongo/">DockerHub</a>. If you need to use a custom MongoDB
9+
* image, you can provide the full image name as well.
10+
*
11+
* @author Stefan Ludwig
12+
*/
13+
public class MongoDbContainer extends GenericContainer<MongoDbContainer> {
14+
15+
/**
16+
* This is the internal port on which MongoDB is running inside the container.
17+
* <p>
18+
* You can use this constant in case you want to map an explicit public port to it
19+
* instead of the default random port. This can be done using methods like
20+
* {@link #setPortBindings(java.util.List)}.
21+
*/
22+
public static final int MONGODB_PORT = 27017;
23+
public static final String DEFAULT_IMAGE_AND_TAG = "mongo:4.0";
24+
25+
/**
26+
* Creates a new {@link MongoDbContainer} with the {@value DEFAULT_IMAGE_AND_TAG} image.
27+
*/
28+
public MongoDbContainer() {
29+
this(DEFAULT_IMAGE_AND_TAG);
30+
}
31+
32+
/**
33+
* Creates a new {@link MongoDbContainer} with the given {@code 'image'}.
34+
*
35+
* @param image the image (e.g. {@value DEFAULT_IMAGE_AND_TAG}) to use
36+
*/
37+
public MongoDbContainer(@NotNull String image) {
38+
super(image);
39+
addExposedPort(MONGODB_PORT);
40+
}
41+
42+
/**
43+
* Returns the actual public port of the internal MongoDB port ({@value MONGODB_PORT}).
44+
*
45+
* @return the public port of this container
46+
* @see #getMappedPort(int)
47+
*/
48+
@NotNull
49+
public Integer getPort() {
50+
return getMappedPort(MONGODB_PORT);
51+
}
52+
53+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.testcontainers.containers;
2+
3+
import java.io.IOException;
4+
import java.net.Socket;
5+
6+
import org.junit.Test;
7+
8+
9+
public class MongoDbContainerTest {
10+
11+
@Test
12+
public void containerStartsAndPublicPortIsAvailable() {
13+
try (MongoDbContainer container = new MongoDbContainer()) {
14+
container.start();
15+
assertThatPortIsAvailable(container);
16+
}
17+
}
18+
19+
private void assertThatPortIsAvailable(MongoDbContainer container) {
20+
try {
21+
new Socket(container.getContainerIpAddress(), container.getPort());
22+
} catch (IOException e) {
23+
throw new AssertionError("The expected port " + container.getPort() + " is not available!");
24+
}
25+
}
26+
27+
}

examples/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ includeBuild '..'
55
// explicit include to allow Dependabot to autodiscover subprojects
66
include 'disque-job-queue'
77
include 'linked-container'
8+
include 'mongodb-container'
89
include 'redis-backed-cache'
910
include 'redis-backed-cache-testng'
1011
include 'selenium-container'

0 commit comments

Comments
 (0)