Skip to content

Commit ec8892e

Browse files
committed
Extract Common Logic for WireMock Test Setup to Share Across Test Cases
Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent 779478f commit ec8892e

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

src/test/java/io/securecodebox/persistence/defectdojo/HttpClientTest.java src/test/java/io/securecodebox/persistence/defectdojo/service/HttpClientExampleTest.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
package io.securecodebox.persistence.defectdojo;
1+
package io.securecodebox.persistence.defectdojo.service;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
4-
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
5+
import io.securecodebox.persistence.defectdojo.config.Config;
56
import org.junit.jupiter.api.Disabled;
67
import org.junit.jupiter.api.Test;
78

89
import java.io.IOException;
910
import java.net.URI;
11+
import java.net.URISyntaxException;
1012
import java.net.http.HttpClient;
1113
import java.net.http.HttpRequest;
1214
import java.net.http.HttpResponse;
@@ -22,18 +24,15 @@
2224
* See https://wiremock.org/docs/quickstart/java-junit/
2325
* and https://wiremock.org/docs/junit-jupiter/
2426
*/
25-
@WireMockTest(httpPort = HttpClientTest.PORT)
26-
final class HttpClientTest {
27-
28-
public static final int PORT = 8888;
27+
final class HttpClientExampleTest extends WireMockBaseTestCase {
2928

3029
private URI createUri(String path) {
3130
return URI.create("http://localhost:%d/%s".formatted(PORT, path));
3231
}
3332

3433
@Test
3534
@Disabled
36-
void test_something_with_wiremock(WireMockRuntimeInfo wmRuntimeInfo) throws IOException, InterruptedException {
35+
void somethingWithWreMock(WireMockRuntimeInfo wmRuntimeInfo) throws IOException, InterruptedException {
3736
stubFor(get("/my/resource")
3837
.withHeader("Content-Type", containing("xml"))
3938
.willReturn(ok()

src/test/java/io/securecodebox/persistence/defectdojo/service/ProductTypeServiceTest.java

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package io.securecodebox.persistence.defectdojo.service;
22

3-
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
4-
import io.securecodebox.persistence.defectdojo.config.Config;
53
import io.securecodebox.persistence.defectdojo.model.ProductType;
64
import org.junit.jupiter.api.Test;
75

86
import java.io.IOException;
97
import java.net.URISyntaxException;
10-
import java.nio.charset.StandardCharsets;
11-
import java.util.Objects;
128

139
import static com.github.tomakehurst.wiremock.client.WireMock.*;
1410
import static org.hamcrest.MatcherAssert.assertThat;
@@ -19,20 +15,8 @@
1915
/**
2016
* Tests for {@link ProductTypeService}
2117
*/
22-
@WireMockTest(httpPort = ProductTypeServiceTest.PORT)
23-
class ProductTypeServiceTest {
24-
public static final int PORT = 8888;
25-
private final Config conf = new Config(
26-
String.format("http://localhost:%d/", PORT),
27-
"not-required-for-tests");
28-
private final ProductTypeService sut = new ProductTypeService(conf);
29-
30-
private String readResponseBodyFromFixture(String name) throws IOException {
31-
try (final var input = getClass().getClassLoader().getResourceAsStream(name)) {
32-
final var bytes = Objects.requireNonNull(input).readAllBytes();
33-
return new String(bytes, StandardCharsets.UTF_8);
34-
}
35-
}
18+
final class ProductTypeServiceTest extends WireMockBaseTestCase {
19+
private final ProductTypeService sut = new ProductTypeService(conf());
3620

3721
@Test
3822
void search() throws URISyntaxException, IOException {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.securecodebox.persistence.defectdojo.service;
2+
3+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
4+
import io.securecodebox.persistence.defectdojo.config.Config;
5+
import lombok.Getter;
6+
import lombok.experimental.Accessors;
7+
8+
import java.io.IOException;
9+
import java.nio.charset.StandardCharsets;
10+
import java.util.Objects;
11+
12+
/**
13+
* Base test class for WireMock test setups
14+
*/
15+
@Getter
16+
@Accessors(fluent = true)
17+
@WireMockTest(httpPort = WireMockBaseTestCase.PORT)
18+
abstract class WireMockBaseTestCase {
19+
static final int PORT = 8888;
20+
21+
private final Config conf = new Config(
22+
String.format("http://localhost:%d/", PORT),
23+
"not-required-for-tests");
24+
25+
String readResponseBodyFromFixture(String fixtureFilePath) throws IOException {
26+
try (final var input = getClass().getClassLoader().getResourceAsStream(fixtureFilePath)) {
27+
final var bytes = Objects.requireNonNull(input).readAllBytes();
28+
return new String(bytes, StandardCharsets.UTF_8);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)