Skip to content

Commit 0a357ff

Browse files
authored
Mockserver module (#696)
* Add MockServer module * CHANGELOG.md * remove delegate
1 parent ca2dd7a commit 0a357ff

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
88

99
### Changed
1010
- Added InfluxDB module ([\#686](https://github.com/testcontainers/testcontainers-java/pull/686))
11+
- Added MockServer module ([\#696](https://github.com/testcontainers/testcontainers-java/pull/696))
1112

1213
## [1.7.2] - 2018-04-30
1314

modules/mockserver/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
description = "Testcontainers :: MockServer"
2+
3+
dependencies {
4+
compile project(':testcontainers')
5+
6+
compileOnly 'org.mock-server:mockserver-client-java:5.3.0'
7+
testCompile 'org.mock-server:mockserver-client-java:5.3.0'
8+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.testcontainers.containers;
2+
3+
import com.github.dockerjava.api.command.InspectContainerResponse;
4+
import lombok.Getter;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.mockserver.client.server.MockServerClient;
7+
8+
@Slf4j
9+
public class MockServerContainer extends GenericContainer<MockServerContainer> {
10+
11+
public static final String VERSION = "5.3.0";
12+
13+
public static final int PORT = 80;
14+
15+
@Getter
16+
private MockServerClient client;
17+
18+
public MockServerContainer() {
19+
this(VERSION);
20+
}
21+
22+
public MockServerContainer(String version) {
23+
super("jamesdbloom/mockserver:mockserver-" + version);
24+
withCommand("/opt/mockserver/run_mockserver.sh -logLevel INFO -serverPort " + PORT);
25+
addExposedPorts(PORT);
26+
}
27+
28+
@Override
29+
protected void containerIsStarted(InspectContainerResponse containerInfo) {
30+
super.containerIsStarted(containerInfo);
31+
32+
client = new MockServerClient(getContainerIpAddress(), getMappedPort(PORT));
33+
}
34+
35+
public String getEndpoint() {
36+
return String.format("http://%s:%d", getContainerIpAddress(), getMappedPort(PORT));
37+
}
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.testcontainers.containers;
2+
3+
import lombok.Cleanup;
4+
import org.junit.ClassRule;
5+
import org.junit.Test;
6+
7+
import java.io.BufferedReader;
8+
import java.io.InputStreamReader;
9+
import java.net.URL;
10+
import java.net.URLConnection;
11+
12+
import static org.mockserver.model.HttpRequest.request;
13+
import static org.mockserver.model.HttpResponse.response;
14+
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;
15+
16+
public class MockServerContainerTest {
17+
18+
@ClassRule
19+
public static MockServerContainer mockServer = new MockServerContainer();
20+
21+
@Test
22+
public void testBasicScenario() throws Exception {
23+
mockServer.getClient()
24+
.when(request("/hello"))
25+
.respond(response("Hello World!"));
26+
27+
URLConnection urlConnection = new URL(mockServer.getEndpoint() + "/hello").openConnection();
28+
@Cleanup BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
29+
String line = reader.readLine();
30+
System.out.println(line);
31+
32+
assertTrue("MockServer returns correct result", line.contains("Hello World!"));
33+
}
34+
}

0 commit comments

Comments
 (0)