4
4
import com .github .tomakehurst .wiremock .core .WireMockConfiguration ;
5
5
import org .junit .jupiter .api .BeforeEach ;
6
6
import org .junit .jupiter .api .Test ;
7
+ import org .junit .jupiter .api .extension .ExtensionContext ;
7
8
import org .junit .jupiter .api .extension .ParameterResolutionException ;
8
9
import ru .lanwen .wiremock .config .CustomizationContext .CustomizationContextBuilder ;
9
10
import ru .lanwen .wiremock .config .WiremockConfigFactory ;
10
11
import ru .lanwen .wiremock .config .WiremockCustomizer ;
11
12
import ru .lanwen .wiremock .ext .WiremockResolver .Wiremock ;
12
13
13
14
import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .options ;
15
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
14
16
import static org .junit .jupiter .api .Assertions .assertNotNull ;
15
17
import static org .junit .jupiter .api .Assertions .assertNotSame ;
16
18
import static org .junit .jupiter .api .Assertions .assertSame ;
17
19
import static org .junit .jupiter .api .Assertions .assertThrows ;
20
+ import static org .junit .jupiter .api .extension .ExtensionContext .Namespace ;
21
+ import static org .junit .jupiter .api .extension .ExtensionContext .Store ;
18
22
import static org .mockito .Mockito .mock ;
19
23
import static org .mockito .Mockito .when ;
20
24
@@ -36,6 +40,15 @@ public void customize(WireMockServer server) {
36
40
}
37
41
}
38
42
43
+ public static class FactoryUsingContext implements WiremockConfigFactory {
44
+ @ Override
45
+ public WireMockConfiguration create (ExtensionContext context ) {
46
+ Integer port = context .getStore (Namespace .GLOBAL )
47
+ .get ("port" , Integer .class );
48
+ return options ().port (port );
49
+ }
50
+ }
51
+
39
52
private static class PrivateClassNotAllowed implements WiremockConfigFactory , WiremockCustomizer {
40
53
41
54
@ Override
@@ -55,12 +68,12 @@ public void customize(WireMockServer server) {
55
68
56
69
@ BeforeEach
57
70
public void setup () {
58
- when (mockedServer .factory ()).thenReturn ((Class ) StubClass .class );
59
71
when (mockedServer .customizer ()).thenReturn ((Class ) StubClass .class );
60
72
}
61
73
62
74
@ Test
63
75
public void createServer () {
76
+ when (mockedServer .factory ()).thenReturn ((Class ) StubClass .class );
64
77
WireMockServer srv1 = factory .createServer (mockedServer );
65
78
WireMockServer srv2 = factory .createServer (mockedServer );
66
79
assertNotNull (srv1 );
@@ -70,11 +83,29 @@ public void createServer() {
70
83
assertNotSame (srv1 , srv2 );
71
84
}
72
85
86
+ @ Test
87
+ public void createServerWithContext () {
88
+ ExtensionContext ctx = mock (ExtensionContext .class );
89
+ Store store = mock (Store .class );
90
+ when (ctx .getStore (Namespace .GLOBAL )).thenReturn (store );
91
+ when (store .get ("port" , Integer .class )).thenReturn (9874 );
92
+
93
+ when (mockedServer .factory ()).thenReturn ((Class ) FactoryUsingContext .class );
94
+
95
+ WireMockServer srv1 = factory .createServer (mockedServer , ctx );
96
+ WireMockServer srv2 = factory .createServer (mockedServer , ctx );
97
+ assertNotNull (srv1 );
98
+ assertNotNull (srv2 );
99
+ assertEquals (9874 , srv1 .getOptions ().portNumber ());
100
+ assertEquals (9874 , srv2 .getOptions ().portNumber ());
101
+ assertNotSame (srv1 , srv2 );
102
+ }
103
+
73
104
@ Test
74
105
public void configFactoryCouldNotBeInstantiated () {
75
106
when (mockedServer .factory ()).thenReturn ((Class ) PrivateClassNotAllowed .class );
76
107
assertThrows (ParameterResolutionException .class ,
77
- () -> factory .createServer (mockedServer ),
108
+ () -> factory .createServer (mockedServer , null ),
78
109
"Can't create config with given factory class ru.lanwen.wiremock.ext.WiremockFactoryTest$PrivateClassNotAllowed" );
79
110
}
80
111
0 commit comments