Skip to content

Commit 22deceb

Browse files
committed
Migrate tests to ApplicationContextRunner
1 parent 62d70da commit 22deceb

16 files changed

+778
-804
lines changed

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfigurationTests.java

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import javax.management.ObjectInstance;
2525
import javax.management.ObjectName;
2626

27-
import org.junit.After;
28-
import org.junit.Before;
2927
import org.junit.Rule;
3028
import org.junit.Test;
3129
import org.junit.rules.ExpectedException;
@@ -34,14 +32,14 @@
3432
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3533
import org.springframework.boot.WebApplicationType;
3634
import org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar;
35+
import org.springframework.boot.autoconfigure.AutoConfigurations;
3736
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
3837
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
3938
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
4039
import org.springframework.boot.builder.SpringApplicationBuilder;
41-
import org.springframework.boot.test.util.TestPropertyValues;
40+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
4241
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
4342
import org.springframework.context.ConfigurableApplicationContext;
44-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4543

4644
import static org.assertj.core.api.Assertions.assertThat;
4745
import static org.junit.Assert.fail;
@@ -56,52 +54,42 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
5654

5755
private static final String ENABLE_ADMIN_PROP = "spring.application.admin.enabled=true";
5856

59-
private static final String JMX_NAME_PROPERTY = "spring.application.admin.jmx-name";
60-
6157
private static final String DEFAULT_JMX_NAME = "org.springframework.boot:type=Admin,name=SpringApplication";
6258

6359
@Rule
6460
public final ExpectedException thrown = ExpectedException.none();
6561

66-
private ConfigurableApplicationContext context;
67-
68-
private MBeanServer mBeanServer;
62+
private final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
6963

70-
@Before
71-
public void setup() throws MalformedObjectNameException {
72-
this.mBeanServer = ManagementFactory.getPlatformMBeanServer();
73-
}
74-
75-
@After
76-
public void tearDown() {
77-
if (this.context != null) {
78-
this.context.close();
79-
}
80-
}
64+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
65+
.withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class,
66+
SpringApplicationAdminJmxAutoConfiguration.class));
8167

8268
@Test
8369
public void notRegisteredByDefault()
8470
throws MalformedObjectNameException, InstanceNotFoundException {
85-
load();
86-
this.thrown.expect(InstanceNotFoundException.class);
87-
this.mBeanServer.getObjectInstance(createDefaultObjectName());
71+
this.contextRunner.run((context) -> {
72+
this.thrown.expect(InstanceNotFoundException.class);
73+
this.mBeanServer.getObjectInstance(createDefaultObjectName());
74+
});
8875
}
8976

9077
@Test
9178
public void registeredWithProperty() throws Exception {
92-
load(ENABLE_ADMIN_PROP);
93-
ObjectName objectName = createDefaultObjectName();
94-
ObjectInstance objectInstance = this.mBeanServer.getObjectInstance(objectName);
95-
assertThat(objectInstance).as("Lifecycle bean should have been registered")
96-
.isNotNull();
79+
this.contextRunner.withPropertyValues(ENABLE_ADMIN_PROP).run((context) -> {
80+
ObjectName objectName = createDefaultObjectName();
81+
ObjectInstance objectInstance = this.mBeanServer.getObjectInstance(objectName);
82+
assertThat(objectInstance).as("Lifecycle bean should have been registered")
83+
.isNotNull();
84+
});
9785
}
9886

9987
@Test
10088
public void registerWithCustomJmxName() throws InstanceNotFoundException {
10189
String customJmxName = "org.acme:name=FooBar";
102-
System.setProperty(JMX_NAME_PROPERTY, customJmxName);
103-
try {
104-
load(ENABLE_ADMIN_PROP);
90+
this.contextRunner.withSystemProperties(
91+
"spring.application.admin.jmx-name=" + customJmxName)
92+
.withPropertyValues(ENABLE_ADMIN_PROP).run((context) -> {
10593
try {
10694
this.mBeanServer.getObjectInstance(createObjectName(customJmxName));
10795
}
@@ -110,27 +98,25 @@ public void registerWithCustomJmxName() throws InstanceNotFoundException {
11098
}
11199
this.thrown.expect(InstanceNotFoundException.class); // Should not be exposed
112100
this.mBeanServer.getObjectInstance(createDefaultObjectName());
113-
}
114-
finally {
115-
System.clearProperty(JMX_NAME_PROPERTY);
116-
}
101+
});
117102
}
118103

119104
@Test
120105
public void registerWithSimpleWebApp() throws Exception {
121-
this.context = new SpringApplicationBuilder()
106+
try (ConfigurableApplicationContext context = new SpringApplicationBuilder()
122107
.sources(ServletWebServerFactoryAutoConfiguration.class,
123108
DispatcherServletAutoConfiguration.class,
124109
JmxAutoConfiguration.class,
125110
SpringApplicationAdminJmxAutoConfiguration.class)
126-
.run("--" + ENABLE_ADMIN_PROP, "--server.port=0");
127-
assertThat(this.context).isInstanceOf(ServletWebServerApplicationContext.class);
128-
assertThat(this.mBeanServer.getAttribute(createDefaultObjectName(),
129-
"EmbeddedWebApplication")).isEqualTo(Boolean.TRUE);
130-
int expected = ((ServletWebServerApplicationContext) this.context).getWebServer()
131-
.getPort();
132-
String actual = getProperty(createDefaultObjectName(), "local.server.port");
133-
assertThat(actual).isEqualTo(String.valueOf(expected));
111+
.run("--" + ENABLE_ADMIN_PROP, "--server.port=0")) {
112+
assertThat(context).isInstanceOf(ServletWebServerApplicationContext.class);
113+
assertThat(this.mBeanServer.getAttribute(createDefaultObjectName(),
114+
"EmbeddedWebApplication")).isEqualTo(Boolean.TRUE);
115+
int expected = ((ServletWebServerApplicationContext) context).getWebServer()
116+
.getPort();
117+
String actual = getProperty(createDefaultObjectName(), "local.server.port");
118+
assertThat(actual).isEqualTo(String.valueOf(expected));
119+
}
134120
}
135121

136122
@Test
@@ -145,8 +131,8 @@ public void onlyRegisteredOnceWhenThereIsAChildContext() throws Exception {
145131

146132
try (ConfigurableApplicationContext parent = parentBuilder
147133
.run("--" + ENABLE_ADMIN_PROP);
148-
ConfigurableApplicationContext child = childBuilder
149-
.run("--" + ENABLE_ADMIN_PROP)) {
134+
ConfigurableApplicationContext child = childBuilder
135+
.run("--" + ENABLE_ADMIN_PROP)) {
150136
BeanFactoryUtils.beanOfType(parent.getBeanFactory(),
151137
SpringApplicationAdminMXBeanRegistrar.class);
152138
this.thrown.expect(NoSuchBeanDefinitionException.class);
@@ -173,13 +159,4 @@ private String getProperty(ObjectName objectName, String key) throws Exception {
173159
new Object[] { key }, new String[] { String.class.getName() });
174160
}
175161

176-
private void load(String... environment) {
177-
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
178-
TestPropertyValues.of(environment).applyTo(applicationContext);
179-
applicationContext.register(JmxAutoConfiguration.class,
180-
SpringApplicationAdminJmxAutoConfiguration.class);
181-
applicationContext.refresh();
182-
this.context = applicationContext;
183-
}
184-
185162
}

0 commit comments

Comments
 (0)