Skip to content

Commit

Permalink
Merge pull request #49 from bitxon/feature/junit-5-as-base-framework
Browse files Browse the repository at this point in the history
Use Junit5 as base test framework
  • Loading branch information
oleg-nenashev committed May 27, 2023
2 parents 9db2cb0 + dfdd22a commit 8a89639
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 240 deletions.
163 changes: 85 additions & 78 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,32 @@ P.S: Javadoc is coming soon!
#### Sample Code using JUnit 5

```java
import static org.assertj.core.api.Assertions.assertThat;

import java.net.http.*;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.testcontainers.junit.jupiter.*;
import org.wiremock.integrations.testcontainers.testsupport.http.*;

import static org.assertj.core.api.Assertions.assertThat;

@Testcontainers
public class WireMockContainerJUnit5Test {

@Container
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
"hello-world-resource-response.xml");

@Test
public void helloWorld() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(wiremockServer.getUrl("/hello")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.GET().build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

assertThat(response.body())
.as("Wrong response body")
.contains("Hello, world!");
}
class WireMockContainerJunit5Test {

@Container
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
.withMapping("hello", WireMockContainerJunit5Test.class, "hello-world.json");

@Test
void helloWorld() throws Exception {
// given
String url = wiremockServer.getUrl("/hello");

// when
HttpResponse response = new TestHttpClient().get(url);

// then
assertThat(response.getBody())
.as("Wrong response body")
.contains("Hello, world!");
}
}
```

Expand All @@ -82,31 +76,27 @@ Show Code
</summary>

```java
import org.wiremock.integrations.testcontainers.WireMockContainer;
import org.junit.*;
import java.net.URI;
import java.net.http.*;
import java.time.Duration;
import org.wiremock.integrations.testcontainers.testsupport.http.*;

public class WireMockContainerTest {
import static org.assertj.core.api.Assertions.assertThat;

public class WireMockContainerJunit4Test {

@Rule
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
.withMapping("hello", WireMockContainerTest.class, "hello-world.json");
.withMapping("hello", WireMockContainerJunit4Test.class, "hello-world.json");

@Test
public void helloWorld() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(wiremockServer.getUrl("/hello")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.GET().build();
// given
String url = wiremockServer.getUrl("/hello");

HttpResponse<String> response =
client.send(request, HttpResponse.BodyHandlers.ofString());
// when
HttpResponse response = new TestHttpClient().get(url);

assertThat(response.body())
// then
assertThat(response.getBody())
.as("Wrong response body")
.contains("Hello, world!");
}
Expand Down Expand Up @@ -188,29 +178,38 @@ Test sample:
##### Sample code using JUnit 5

```java
import org.junit.jupiter.api.*;
import org.testcontainers.junit.jupiter.*;
import org.wiremock.integrations.testcontainers.testsupport.http.*;

import java.nio.file.Paths;
import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;

@Testcontainers
public class WireMockContainerExtensionJUnit5Test {

@Container
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")
.withExtension("JSON Body Transformer", Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
Collections.singleton(Paths.get("target", "test-wiremock-extension", "9cookies-wiremock-extensions.jar").toFile()));

@Test
public void testJSONBodyTransformer() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(wiremockServer.getUrl("/json-body-transformer")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

assertThat(response.body()).as("Wrong response body")
.contains("Hello, John Doe!");
}
class WireMockContainerExtensionJunit5Test {

@Container
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
.withMapping("json-body-transformer", WireMockContainerExtensionJunit5Test.class, "json-body-transformer.json")
.withExtension("JSON Body Transformer",
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));

@Test
void testJSONBodyTransformer() throws Exception {
// given
String url = wiremockServer.getUrl("/json-body-transformer");
String body = "{\"name\":\"John Doe\"}";

// when
HttpResponse response = new TestHttpClient().post(url, body);

// then
assertThat(response.getBody()).as("Wrong response body")
.contains("Hello, John Doe!");
}
}
```

Expand All @@ -222,26 +221,34 @@ Show Code
</summary>

```java
import org.junit.*;
import org.wiremock.integrations.testcontainers.testsupport.http.*;

import java.nio.file.Paths;
import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;

public class WireMockContainerExtensionJunit4Test {

public class WireMockContainerExtensionTest {
@Rule
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")
.withExtension("JSON Body Transformer", Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
Collections.singleton(Paths.get("target", "test-wiremock-extension", "9cookies-wiremock-extensions.jar").toFile()));
.withMapping("json-body-transformer", WireMockContainerExtensionJunit4Test.class, "json-body-transformer.json")
.withExtension("JSON Body Transformer",
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));

@Test
public void testJSONBodyTransformer() throws Exception {
final HttpClient client = HttpClient.newBuilder().build();
final HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(wiremockServer.getUrl("/json-body-transformer")))
.timeout(Duration.ofSeconds(10))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();
// given
String url = wiremockServer.getUrl("/json-body-transformer");
String body = "{\"name\":\"John Doe\"}";

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// when
HttpResponse response = new TestHttpClient().post(url, body);

assertThat(response.body()).as("Wrong response body")
// then
assertThat(response.getBody()).as("Wrong response body")
.contains("Hello, John Doe!");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2023 WireMock Inc, Oleg Nenashev and all project contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wiremock.integrations.testcontainers;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class WireMockContainerBannerTest {

WireMockContainer wireMockContainer = new WireMockContainer("2.35.0");

@Test
void bannerIsByDefaultDisabled() {
// when
wireMockContainer.configure();

// then
assertThat(wireMockContainer.getCommandParts())
.contains("--disable-banner");
}

@Test
void enableBanner() {
// given
wireMockContainer.withBanner();

// when
wireMockContainer.configure();

// then
assertThat(wireMockContainer.getCommandParts())
.doesNotContain("--disable-banner");
}

@Test
void disableBanner() {
// given
wireMockContainer.withoutBanner();

// when
wireMockContainer.configure();

// then
assertThat(wireMockContainer.getCommandParts())
.contains("--disable-banner");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package org.wiremock.integrations.testcontainers;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;

Expand All @@ -34,25 +34,22 @@
* Tests the WireMock extension loading.
* It uses the external Jar supplied by the Maven Dependency Plugin.
*/
public class WireMockContainerExtensionTest {
@Testcontainers
class WireMockContainerExtensionTest {

public static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionTest.class);
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionTest.class);

@Rule
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
@Container
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
.withStartupTimeout(Duration.ofSeconds(60))
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")
.withExtension("JSON Body Transformer",
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));

@Before
public void before() {
wiremockServer.followOutput(new Slf4jLogConsumer(LOGGER));
}

@Test
public void testJSONBodyTransformer() throws Exception {
void testJSONBodyTransformer() throws Exception {
// given
String url = wiremockServer.getUrl("/json-body-transformer");
String body = "{\"name\":\"John Doe\"}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,32 @@
*/
package org.wiremock.integrations.testcontainers;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;

import java.nio.file.Paths;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests the WireMock extension loading.
* It uses multiple external Jars supplied by the Maven Dependency Plugin.
*/
public class WireMockContainerExtensionsCombinationTest {
@Testcontainers
class WireMockContainerExtensionsCombinationTest {

public static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionsCombinationTest.class);
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionsCombinationTest.class);

@Rule
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
@Container
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
.withMapping("json-body-transformer", WireMockContainerExtensionsCombinationTest.class, "json-body-transformer.json")
.withExtension("Webhook",
Collections.singleton("org.wiremock.webhooks.Webhooks"),
Expand All @@ -48,13 +49,8 @@ public class WireMockContainerExtensionsCombinationTest {
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));

@Before
public void before() {
wiremockServer.followOutput(new Slf4jLogConsumer(LOGGER));
}

@Test
public void testJSONBodyTransformer() throws Exception {
void testJSONBodyTransformer() throws Exception {
// given
String url = wiremockServer.getUrl("/json-body-transformer");
String body = "{\"name\":\"John Doe\"}";
Expand Down
Loading

0 comments on commit 8a89639

Please sign in to comment.