Skip to content

Commit acfac5b

Browse files
committed
refactor: Refactor AbstractClientApplicationTest and its inheritors
1 parent 0cc24a8 commit acfac5b

File tree

3 files changed

+49
-80
lines changed

3 files changed

+49
-80
lines changed

spring-boot-admin-client/src/test/java/de/codecentric/boot/admin/client/AbstractClientApplicationTest.java

+45-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import java.net.InetAddress;
2020
import java.net.UnknownHostException;
21+
import java.util.Arrays;
2122
import java.util.concurrent.CountDownLatch;
23+
import java.util.stream.Stream;
2224

2325
import com.github.tomakehurst.wiremock.WireMockServer;
2426
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
@@ -27,9 +29,12 @@
2729
import org.junit.jupiter.api.AfterEach;
2830
import org.junit.jupiter.api.Test;
2931
import org.springframework.beans.factory.annotation.Autowired;
32+
import org.springframework.boot.SpringApplication;
3033
import org.springframework.boot.SpringBootConfiguration;
34+
import org.springframework.boot.WebApplicationType;
3135
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3236
import org.springframework.boot.context.event.ApplicationReadyEvent;
37+
import org.springframework.context.ConfigurableApplicationContext;
3338
import org.springframework.context.event.EventListener;
3439

3540
import de.codecentric.boot.admin.client.registration.ApplicationRegistrator;
@@ -45,11 +50,21 @@
4550

4651
public 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

spring-boot-admin-client/src/test/java/de/codecentric/boot/admin/client/ClientReactiveApplicationTest.java

+2-38
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,14 @@
1616

1717
package de.codecentric.boot.admin.client;
1818

19-
import org.junit.jupiter.api.AfterEach;
2019
import org.junit.jupiter.api.BeforeEach;
21-
import org.junit.jupiter.api.TestInfo;
22-
import org.springframework.boot.SpringApplication;
2320
import org.springframework.boot.WebApplicationType;
24-
import org.springframework.context.ConfigurableApplicationContext;
2521

2622
public class ClientReactiveApplicationTest extends AbstractClientApplicationTest {
2723

28-
private ConfigurableApplicationContext instance;
29-
3024
@BeforeEach
31-
public void setUp(TestInfo testInfo) throws Exception {
32-
super.setUp();
33-
34-
SpringApplication application = new SpringApplication(TestClientApplication.class);
35-
application.setWebApplicationType(WebApplicationType.REACTIVE);
36-
if (testInfo.getDisplayName().equalsIgnoreCase("test_context_with_snake_case()")) {
37-
instance = application.run("--spring.application.name=Test-Client", "--server.port=0",
38-
"--management.endpoints.web.base-path=/mgmt", "--endpoints.health.enabled=true",
39-
"--spring.boot.admin.client.url=" + wireMock.url("/"),
40-
"--spring.jackson.property-naming-strategy=SNAKE_CASE");
41-
}
42-
else {
43-
instance = application.run("--spring.application.name=Test-Client", "--server.port=0",
44-
"--management.endpoints.web.base-path=/mgmt", "--endpoints.health.enabled=true",
45-
"--spring.boot.admin.client.url=" + wireMock.url("/"));
46-
}
47-
}
48-
49-
@AfterEach
50-
public void shutdown() {
51-
instance.close();
52-
}
53-
54-
@Override
55-
protected int getServerPort() {
56-
return instance.getEnvironment().getProperty("local.server.port", Integer.class, 0);
57-
}
58-
59-
@Override
60-
protected int getManagementPort() {
61-
return instance.getEnvironment().getProperty("local.management.port", Integer.class, 0);
62-
25+
public void setUp() throws Exception {
26+
super.setUp(WebApplicationType.REACTIVE);
6327
}
6428

6529
}

spring-boot-admin-client/src/test/java/de/codecentric/boot/admin/client/ClientServletApplicationTest.java

+2-38
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,14 @@
1616

1717
package de.codecentric.boot.admin.client;
1818

19-
import org.junit.jupiter.api.AfterEach;
2019
import org.junit.jupiter.api.BeforeEach;
21-
import org.junit.jupiter.api.TestInfo;
22-
import org.springframework.boot.SpringApplication;
2320
import org.springframework.boot.WebApplicationType;
24-
import org.springframework.context.ConfigurableApplicationContext;
2521

2622
public class ClientServletApplicationTest extends AbstractClientApplicationTest {
2723

28-
private ConfigurableApplicationContext instance;
29-
3024
@BeforeEach
31-
public void setUp(TestInfo testInfo) throws Exception {
32-
super.setUp();
33-
34-
SpringApplication application = new SpringApplication(TestClientApplication.class);
35-
application.setWebApplicationType(WebApplicationType.SERVLET);
36-
if (testInfo.getDisplayName().equalsIgnoreCase("test_context_with_snake_case()")) {
37-
instance = application.run("--spring.application.name=Test-Client", "--server.port=0",
38-
"--management.endpoints.web.base-path=/mgmt", "--endpoints.health.enabled=true",
39-
"--spring.boot.admin.client.url=" + wireMock.url("/"),
40-
"--spring.jackson.property-naming-strategy=SNAKE_CASE");
41-
}
42-
else {
43-
instance = application.run("--spring.application.name=Test-Client", "--server.port=0",
44-
"--management.endpoints.web.base-path=/mgmt", "--endpoints.health.enabled=true",
45-
"--spring.boot.admin.client.url=" + wireMock.url("/"));
46-
}
47-
}
48-
49-
@AfterEach
50-
public void shutdown() {
51-
instance.close();
52-
}
53-
54-
@Override
55-
protected int getServerPort() {
56-
return instance.getEnvironment().getProperty("local.server.port", Integer.class, 0);
57-
}
58-
59-
@Override
60-
protected int getManagementPort() {
61-
return instance.getEnvironment().getProperty("local.management.port", Integer.class, 0);
62-
25+
public void setUp() throws Exception {
26+
super.setUp(WebApplicationType.SERVLET);
6327
}
6428

6529
}

0 commit comments

Comments
 (0)