Skip to content

Commit f3de9ae

Browse files
patowntommysitu
authored andcommitted
Add HoverflySpy to enable SPY mode in hoverfly-java-junit5 (#256)
1 parent 7a795e8 commit f3de9ae

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@
33
import io.specto.hoverfly.junit.core.Hoverfly;
44
import io.specto.hoverfly.junit.core.HoverflyMode;
55
import io.specto.hoverfly.junit.core.SimulationSource;
6-
import io.specto.hoverfly.junit5.api.HoverflyCapture;
7-
import io.specto.hoverfly.junit5.api.HoverflyConfig;
8-
import io.specto.hoverfly.junit5.api.HoverflyCore;
9-
import io.specto.hoverfly.junit5.api.HoverflyDiff;
10-
import io.specto.hoverfly.junit5.api.HoverflySimulate;
11-
import io.specto.hoverfly.junit5.api.HoverflyValidate;
6+
import io.specto.hoverfly.junit5.api.*;
127
import org.junit.jupiter.api.extension.*;
138

149
import java.lang.reflect.AnnotatedElement;
@@ -111,8 +106,14 @@ public void beforeAll(ExtensionContext context) {
111106
String path = getPath(context, hoverflyDiff.source());
112107
HoverflySimulate.SourceType type = hoverflyDiff.source().type();
113108
source = getSimulationSource(path, type);
109+
} else if (isAnnotated(annotatedElement, HoverflySpy.class)) {
110+
HoverflySpy hoverflySpy = annotatedElement.getAnnotation(HoverflySpy.class);
111+
config = hoverflySpy.config();
112+
mode = HoverflyMode.SPY;
113+
String path = getPath(context, hoverflySpy.source());
114+
HoverflySimulate.SourceType type = hoverflySpy.source().type();
115+
source = getSimulationSource(path, type);
114116
}
115-
116117
if (!isRunning()) {
117118
hoverfly = new Hoverfly(getHoverflyConfigs(config), mode);
118119
hoverfly.start();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.specto.hoverfly.junit5.api;
2+
3+
import io.specto.hoverfly.junit5.HoverflyExtension;
4+
5+
import io.specto.hoverfly.junit5.api.HoverflySimulate.Source;
6+
import java.lang.annotation.ElementType;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.RetentionPolicy;
9+
import java.lang.annotation.Target;
10+
11+
/**
12+
* Annotation used along with {@link HoverflyExtension} to run Hoverfly in spy mode
13+
* In this mode, Hoverfly simulates external APIs if a request match is found in simulation data
14+
* (See Simulate mode), otherwise, the request will be passed through to the real API.
15+
*/
16+
@Target({ElementType.TYPE})
17+
@Retention(RetentionPolicy.RUNTIME)
18+
public @interface HoverflySpy {
19+
20+
/**
21+
* Hoverfly configurations
22+
* @see HoverflyConfig
23+
*/
24+
HoverflyConfig config() default @HoverflyConfig;
25+
26+
/**
27+
* Simulation source to import
28+
* @see Source
29+
*/
30+
Source source() default @Source;
31+
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package io.specto.hoverfly.junit5;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import io.specto.hoverfly.junit.core.Hoverfly;
6+
import io.specto.hoverfly.junit.core.HoverflyMode;
7+
import io.specto.hoverfly.junit5.api.HoverflySimulate;
8+
import io.specto.hoverfly.junit5.api.HoverflySpy;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
12+
@ExtendWith(HoverflyExtension.class)
13+
@HoverflySpy(source = @HoverflySimulate.Source(value = "test-service-https.json", type = HoverflySimulate.SourceType.CLASSPATH)
14+
)
15+
public class HoverflySpyTest {
16+
17+
@Test
18+
void shouldInjectCustomInstanceAsParameterWithRequiredMode(Hoverfly hoverfly) {
19+
assertThat(hoverfly.getMode()).isEqualTo(HoverflyMode.SPY);
20+
}
21+
22+
@Test
23+
void shouldImportSimulationFromCustomSource(Hoverfly hoverfly) {
24+
assertThat(hoverfly.getSimulation().getHoverflyData().getPairs()).isNotEmpty();
25+
}
26+
}

0 commit comments

Comments
 (0)