18
18
19
19
import java .net .InetAddress ;
20
20
import java .net .UnknownHostException ;
21
+ import java .util .Arrays ;
21
22
import java .util .concurrent .CountDownLatch ;
23
+ import java .util .stream .Stream ;
22
24
23
25
import com .github .tomakehurst .wiremock .WireMockServer ;
24
26
import com .github .tomakehurst .wiremock .client .ResponseDefinitionBuilder ;
27
29
import org .junit .jupiter .api .AfterEach ;
28
30
import org .junit .jupiter .api .Test ;
29
31
import org .springframework .beans .factory .annotation .Autowired ;
32
+ import org .springframework .boot .SpringApplication ;
30
33
import org .springframework .boot .SpringBootConfiguration ;
34
+ import org .springframework .boot .WebApplicationType ;
31
35
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
32
36
import org .springframework .boot .context .event .ApplicationReadyEvent ;
37
+ import org .springframework .context .ConfigurableApplicationContext ;
33
38
import org .springframework .context .event .EventListener ;
34
39
35
40
import de .codecentric .boot .admin .client .registration .ApplicationRegistrator ;
45
50
46
51
public abstract class AbstractClientApplicationTest {
47
52
48
- public WireMockServer wireMock = new WireMockServer (options ().dynamicPort ().notifier (new ConsoleNotifier (true )));
53
+ private final WireMockServer wireMock = new WireMockServer (
54
+ options ().dynamicPort ().notifier (new ConsoleNotifier (true )));
55
+
56
+ private SpringApplication application ;
57
+
58
+ private ConfigurableApplicationContext instance ;
49
59
50
60
private static final CountDownLatch cdl = new CountDownLatch (1 );
51
61
52
- public void setUp () throws Exception {
62
+ protected void setUp (WebApplicationType type ) throws Exception {
63
+ setUpWiremock ();
64
+ setUpApplication (type );
65
+ }
66
+
67
+ private void setUpWiremock () {
53
68
wireMock .start ();
54
69
ResponseDefinitionBuilder response = created ().withHeader ("Content-Type" , "application/json" )
55
70
.withHeader ("Connection" , "close" )
@@ -58,13 +73,33 @@ public void setUp() throws Exception {
58
73
wireMock .stubFor (post (urlEqualTo ("/instances" )).willReturn (response ));
59
74
}
60
75
76
+ private void setUpApplication (WebApplicationType type ) {
77
+ application = new SpringApplication (TestClientApplication .class );
78
+ application .setWebApplicationType (type );
79
+ }
80
+
81
+ private void setUpApplicationContext (String ... additionalArgs ) {
82
+ Stream <String > defaultArgs = Stream .of ("--spring.application.name=Test-Client" , "--server.port=0" ,
83
+ "--management.endpoints.web.base-path=/mgmt" , "--endpoints.health.enabled=true" ,
84
+ "--spring.boot.admin.client.url=" + wireMock .url ("/" ));
85
+
86
+ String [] args = Stream .concat (defaultArgs , Arrays .stream (additionalArgs )).toArray (String []::new );
87
+
88
+ this .instance = application .run (args );
89
+ }
90
+
61
91
@ AfterEach
62
92
void tearDown () {
63
93
wireMock .stop ();
94
+ if (instance != null ) {
95
+ instance .close ();
96
+ }
64
97
}
65
98
66
99
@ Test
67
100
public void test_context () throws InterruptedException , UnknownHostException {
101
+ setUpApplicationContext ();
102
+
68
103
cdl .await ();
69
104
Thread .sleep (2500 );
70
105
String hostName = InetAddress .getLocalHost ().getCanonicalHostName ();
@@ -83,6 +118,8 @@ public void test_context() throws InterruptedException, UnknownHostException {
83
118
84
119
@ Test
85
120
public void test_context_with_snake_case () throws InterruptedException , UnknownHostException {
121
+ setUpApplicationContext ("--spring.jackson.property-naming-strategy=SNAKE_CASE" );
122
+
86
123
cdl .await ();
87
124
Thread .sleep (2500 );
88
125
String hostName = InetAddress .getLocalHost ().getCanonicalHostName ();
@@ -99,9 +136,13 @@ public void test_context_with_snake_case() throws InterruptedException, UnknownH
99
136
wireMock .verify (request );
100
137
}
101
138
102
- protected abstract int getServerPort ();
139
+ private int getServerPort () {
140
+ return instance .getEnvironment ().getProperty ("local.server.port" , Integer .class , 0 );
141
+ }
103
142
104
- protected abstract int getManagementPort ();
143
+ private int getManagementPort () {
144
+ return instance .getEnvironment ().getProperty ("local.management.port" , Integer .class , 0 );
145
+ }
105
146
106
147
@ SpringBootConfiguration
107
148
@ EnableAutoConfiguration
0 commit comments