Skip to content

Commit

Permalink
Provide a way to override the default simulation source in junit5 Hov…
Browse files Browse the repository at this point in the history
…erflySimulate annotation
  • Loading branch information
tommysitu committed Aug 26, 2024
1 parent 6be5ac2 commit 7db4e3e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ enum SourceType {
DEFAULT_PATH,
CLASSPATH,
URL,
FILE
FILE,
EMPTY
}
}
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -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"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down

0 comments on commit 7db4e3e

Please sign in to comment.