diff --git a/junit5/src/main/java/io/specto/hoverfly/junit5/HoverflyExtensionUtils.java b/junit5/src/main/java/io/specto/hoverfly/junit5/HoverflyExtensionUtils.java index 51d48858..f6ef92f0 100644 --- a/junit5/src/main/java/io/specto/hoverfly/junit5/HoverflyExtensionUtils.java +++ b/junit5/src/main/java/io/specto/hoverfly/junit5/HoverflyExtensionUtils.java @@ -76,6 +76,9 @@ static SimulationSource getSimulationSource(String value, HoverflySimulate.Sourc case FILE: source = SimulationSource.file(Paths.get(value)); break; + case EMPTY: + source = SimulationSource.empty(); + break; } return source; } diff --git a/junit5/src/main/java/io/specto/hoverfly/junit5/api/HoverflySimulate.java b/junit5/src/main/java/io/specto/hoverfly/junit5/api/HoverflySimulate.java index 30015ea9..1b7305bd 100644 --- a/junit5/src/main/java/io/specto/hoverfly/junit5/api/HoverflySimulate.java +++ b/junit5/src/main/java/io/specto/hoverfly/junit5/api/HoverflySimulate.java @@ -57,6 +57,7 @@ enum SourceType { DEFAULT_PATH, CLASSPATH, URL, - FILE + FILE, + EMPTY } } diff --git a/junit5/src/test/java/io/specto/hoverfly/junit5/HoverflyBaseExtensionTest.java b/junit5/src/test/java/io/specto/hoverfly/junit5/HoverflyBaseExtensionTest.java new file mode 100644 index 00000000..c4ce8fa5 --- /dev/null +++ b/junit5/src/test/java/io/specto/hoverfly/junit5/HoverflyBaseExtensionTest.java @@ -0,0 +1,38 @@ +package io.specto.hoverfly.junit5; + +import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson; + +import io.specto.hoverfly.junit.core.Hoverfly; +import io.specto.hoverfly.junit.core.SslConfigurer; +import java.io.IOException; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +// Should inherit the Hoverfly annotation from the base test. +class HoverflyBaseExtensionTest extends HoverflyBaseTest { + + private static OkHttpClient client; + + @BeforeAll + static void init(Hoverfly hoverfly) { + SslConfigurer sslConfigurer = hoverfly.getSslConfigurer(); + client = new OkHttpClient.Builder() + .sslSocketFactory(sslConfigurer.getSslContext().getSocketFactory(), sslConfigurer.getTrustManager()) + .build(); + } + + @Test + void shouldImportSimulationFromCustomSource() throws IOException { + + final Request request = new Request.Builder().url("https://www.my-test.com/api/bookings/1") + .build(); + + final Response response = client.newCall(request).execute(); + + assertThatJson(response.body().string()).node("bookingId").isStringEqualTo("1"); + } + +} diff --git a/junit5/src/test/java/io/specto/hoverfly/junit5/HoverflyBaseTest.java b/junit5/src/test/java/io/specto/hoverfly/junit5/HoverflyBaseTest.java new file mode 100644 index 00000000..4407b105 --- /dev/null +++ b/junit5/src/test/java/io/specto/hoverfly/junit5/HoverflyBaseTest.java @@ -0,0 +1,21 @@ +package io.specto.hoverfly.junit5; + +import io.specto.hoverfly.junit.core.Hoverfly; +import io.specto.hoverfly.junit.core.SimulationSource; +import io.specto.hoverfly.junit5.api.HoverflySimulate; +import io.specto.hoverfly.junit5.api.HoverflySimulate.Source; +import io.specto.hoverfly.junit5.api.HoverflySimulate.SourceType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ExtendWith; + +@ExtendWith({HoverflyExtension.class}) +@HoverflySimulate(source = @Source(type = SourceType.EMPTY)) +public abstract class HoverflyBaseTest { + + @BeforeEach + void setUp(Hoverfly hoverfly) { + hoverfly.reset(); + hoverfly.simulate(SimulationSource.classpath("test-service-https.json")); + } + +} diff --git a/junit5/src/test/java/io/specto/hoverfly/junit5/HoverflyDefaultsSimulationTest.java b/junit5/src/test/java/io/specto/hoverfly/junit5/HoverflyDefaultsSimulationTest.java index 8ee713fd..c9955b7d 100644 --- a/junit5/src/test/java/io/specto/hoverfly/junit5/HoverflyDefaultsSimulationTest.java +++ b/junit5/src/test/java/io/specto/hoverfly/junit5/HoverflyDefaultsSimulationTest.java @@ -18,7 +18,8 @@ @ExtendWith(HoverflyExtension.class) class HoverflyDefaultsSimulationTest { - private OkHttpClient client = new OkHttpClient(); + private final OkHttpClient client = new OkHttpClient(); + @Test void shouldImportSimulationFromDefaultLocation() throws IOException {