1818
1919import java .net .InetAddress ;
2020import java .net .UnknownHostException ;
21+ import java .util .Arrays ;
2122import java .util .concurrent .CountDownLatch ;
23+ import java .util .stream .Stream ;
2224
2325import com .github .tomakehurst .wiremock .WireMockServer ;
2426import com .github .tomakehurst .wiremock .client .ResponseDefinitionBuilder ;
2729import org .junit .jupiter .api .AfterEach ;
2830import org .junit .jupiter .api .Test ;
2931import org .springframework .beans .factory .annotation .Autowired ;
32+ import org .springframework .boot .SpringApplication ;
3033import org .springframework .boot .SpringBootConfiguration ;
34+ import org .springframework .boot .WebApplicationType ;
3135import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
3236import org .springframework .boot .context .event .ApplicationReadyEvent ;
37+ import org .springframework .context .ConfigurableApplicationContext ;
3338import org .springframework .context .event .EventListener ;
3439
3540import de .codecentric .boot .admin .client .registration .ApplicationRegistrator ;
4550
4651public abstract class AbstractClientApplicationTest {
4752
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 ;
4959
5060 private static final CountDownLatch cdl = new CountDownLatch (1 );
5161
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 () {
5368 wireMock .start ();
5469 ResponseDefinitionBuilder response = created ().withHeader ("Content-Type" , "application/json" )
5570 .withHeader ("Connection" , "close" )
@@ -58,13 +73,33 @@ public void setUp() throws Exception {
5873 wireMock .stubFor (post (urlEqualTo ("/instances" )).willReturn (response ));
5974 }
6075
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+
6191 @ AfterEach
6292 void tearDown () {
6393 wireMock .stop ();
94+ if (instance != null ) {
95+ instance .close ();
96+ }
6497 }
6598
6699 @ Test
67100 public void test_context () throws InterruptedException , UnknownHostException {
101+ setUpApplicationContext ();
102+
68103 cdl .await ();
69104 Thread .sleep (2500 );
70105 String hostName = InetAddress .getLocalHost ().getCanonicalHostName ();
@@ -83,6 +118,8 @@ public void test_context() throws InterruptedException, UnknownHostException {
83118
84119 @ Test
85120 public void test_context_with_snake_case () throws InterruptedException , UnknownHostException {
121+ setUpApplicationContext ("--spring.jackson.property-naming-strategy=SNAKE_CASE" );
122+
86123 cdl .await ();
87124 Thread .sleep (2500 );
88125 String hostName = InetAddress .getLocalHost ().getCanonicalHostName ();
@@ -99,9 +136,13 @@ public void test_context_with_snake_case() throws InterruptedException, UnknownH
99136 wireMock .verify (request );
100137 }
101138
102- protected abstract int getServerPort ();
139+ private int getServerPort () {
140+ return instance .getEnvironment ().getProperty ("local.server.port" , Integer .class , 0 );
141+ }
103142
104- protected abstract int getManagementPort ();
143+ private int getManagementPort () {
144+ return instance .getEnvironment ().getProperty ("local.management.port" , Integer .class , 0 );
145+ }
105146
106147 @ SpringBootConfiguration
107148 @ EnableAutoConfiguration
0 commit comments