Skip to content

Commit a16aef7

Browse files
committed
Provide a way to override the default simulation source in junit5 HoverflySimulate annotation
1 parent 6be5ac2 commit a16aef7

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

junit5/src/main/java/io/specto/hoverfly/junit5/HoverflyExtensionUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ static SimulationSource getSimulationSource(String value, HoverflySimulate.Sourc
7676
case FILE:
7777
source = SimulationSource.file(Paths.get(value));
7878
break;
79+
case EMPTY:
80+
source = SimulationSource.empty();
81+
break;
7982
}
8083
return source;
8184
}

junit5/src/main/java/io/specto/hoverfly/junit5/api/HoverflySimulate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ enum SourceType {
5757
DEFAULT_PATH,
5858
CLASSPATH,
5959
URL,
60-
FILE
60+
FILE,
61+
EMPTY
6162
}
6263
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.specto.hoverfly.junit5;
2+
3+
import static net.javacrumbs.jsonunit.fluent.JsonFluentAssert.assertThatJson;
4+
5+
import io.specto.hoverfly.junit.core.Hoverfly;
6+
import io.specto.hoverfly.junit.core.SslConfigurer;
7+
import java.io.IOException;
8+
import okhttp3.OkHttpClient;
9+
import okhttp3.Request;
10+
import okhttp3.Response;
11+
import org.junit.jupiter.api.BeforeAll;
12+
import org.junit.jupiter.api.Test;
13+
14+
// Should inherit the Hoverfly annotation from the base test.
15+
class HoverflyBaseExtensionTest extends HoverflyBaseTest {
16+
17+
private static OkHttpClient client;
18+
19+
@BeforeAll
20+
static void init(Hoverfly hoverfly) {
21+
SslConfigurer sslConfigurer = hoverfly.getSslConfigurer();
22+
client = new OkHttpClient.Builder()
23+
.sslSocketFactory(sslConfigurer.getSslContext().getSocketFactory(), sslConfigurer.getTrustManager())
24+
.build();
25+
}
26+
27+
@Test
28+
void shouldImportSimulationFromCustomSource() throws IOException {
29+
30+
final Request request = new Request.Builder().url("https://www.my-test.com/api/bookings/1")
31+
.build();
32+
33+
final Response response = client.newCall(request).execute();
34+
35+
assertThatJson(response.body().string()).node("bookingId").isStringEqualTo("1");
36+
}
37+
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.specto.hoverfly.junit5;
2+
3+
import io.specto.hoverfly.junit.core.Hoverfly;
4+
import io.specto.hoverfly.junit.core.SimulationSource;
5+
import io.specto.hoverfly.junit5.api.HoverflySimulate;
6+
import io.specto.hoverfly.junit5.api.HoverflySimulate.Source;
7+
import io.specto.hoverfly.junit5.api.HoverflySimulate.SourceType;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
11+
@ExtendWith({HoverflyExtension.class})
12+
@HoverflySimulate(source = @Source(type = SourceType.EMPTY))
13+
public abstract class HoverflyBaseTest {
14+
15+
@BeforeEach
16+
void setUp(Hoverfly hoverfly) {
17+
hoverfly.reset();
18+
hoverfly.simulate(SimulationSource.classpath("test-service-https.json"));
19+
}
20+
21+
}

junit5/src/test/java/io/specto/hoverfly/junit5/HoverflyDefaultsSimulationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
@ExtendWith(HoverflyExtension.class)
1919
class HoverflyDefaultsSimulationTest {
2020

21-
private OkHttpClient client = new OkHttpClient();
21+
private final OkHttpClient client = new OkHttpClient();
22+
2223
@Test
2324
void shouldImportSimulationFromDefaultLocation() throws IOException {
2425

0 commit comments

Comments
 (0)