Skip to content

Commit 74cede5

Browse files
committed
Migrate integration tests to context runner
Migrate `IntegrationAutoConfigurationTests` to use the `ApplicationContextRunner`.
1 parent 728b522 commit 74cede5

File tree

1 file changed

+103
-122
lines changed

1 file changed

+103
-122
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfigurationTests.java

Lines changed: 103 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,20 @@
1616

1717
package org.springframework.boot.autoconfigure.integration;
1818

19-
import java.util.Arrays;
20-
import java.util.List;
21-
2219
import javax.management.MBeanServer;
2320

24-
import org.junit.After;
2521
import org.junit.Rule;
2622
import org.junit.Test;
2723
import org.junit.rules.ExpectedException;
2824

25+
import org.springframework.boot.autoconfigure.AutoConfigurations;
2926
import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration.IntegrationComponentScanAutoConfiguration;
3027
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
3128
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
3229
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
3330
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
34-
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
3531
import org.springframework.boot.jdbc.DataSourceInitializationMode;
36-
import org.springframework.boot.test.util.TestPropertyValues;
37-
import org.springframework.context.ConfigurableApplicationContext;
38-
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
32+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3933
import org.springframework.context.annotation.Bean;
4034
import org.springframework.context.annotation.Configuration;
4135
import org.springframework.context.annotation.Primary;
@@ -47,7 +41,6 @@
4741
import org.springframework.jdbc.BadSqlGrammarException;
4842
import org.springframework.jdbc.core.JdbcOperations;
4943
import org.springframework.jmx.export.MBeanExporter;
50-
import org.springframework.test.context.support.TestPropertySourceUtils;
5144

5245
import static org.assertj.core.api.Assertions.assertThat;
5346
import static org.mockito.Mockito.mock;
@@ -64,158 +57,146 @@ public class IntegrationAutoConfigurationTests {
6457
@Rule
6558
public ExpectedException thrown = ExpectedException.none();
6659

67-
private AnnotationConfigApplicationContext context;
68-
69-
@After
70-
public void close() {
71-
if (this.context != null) {
72-
this.context.close();
73-
if (this.context.getParent() != null) {
74-
((ConfigurableApplicationContext) this.context.getParent()).close();
75-
}
76-
}
77-
}
60+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
61+
.withConfiguration(AutoConfigurations.of(JmxAutoConfiguration.class,
62+
IntegrationAutoConfiguration.class));
7863

7964
@Test
8065
public void integrationIsAvailable() {
81-
load();
82-
assertThat(this.context.getBean(TestGateway.class)).isNotNull();
83-
assertThat(this.context.getBean(IntegrationComponentScanAutoConfiguration.class))
84-
.isNotNull();
66+
this.contextRunner.run((context) -> {
67+
assertThat(context).hasSingleBean(TestGateway.class);
68+
assertThat(context)
69+
.hasSingleBean(IntegrationComponentScanAutoConfiguration.class);
70+
});
8571
}
8672

8773
@Test
8874
public void explicitIntegrationComponentScan() {
89-
this.context = new AnnotationConfigApplicationContext();
90-
this.context.register(IntegrationComponentScanConfiguration.class,
91-
JmxAutoConfiguration.class, IntegrationAutoConfiguration.class);
92-
this.context.refresh();
93-
assertThat(this.context.getBean(TestGateway.class)).isNotNull();
94-
assertThat(this.context
95-
.getBeansOfType(IntegrationComponentScanAutoConfiguration.class))
96-
.isEmpty();
75+
this.contextRunner
76+
.withUserConfiguration(IntegrationComponentScanConfiguration.class)
77+
.run((context) -> {
78+
assertThat(context).hasSingleBean(TestGateway.class);
79+
assertThat(context).doesNotHaveBean(
80+
IntegrationComponentScanAutoConfiguration.class);
81+
});
9782
}
9883

9984
@Test
10085
public void parentContext() {
101-
load();
102-
AnnotationConfigApplicationContext parent = this.context;
103-
this.context = new AnnotationConfigApplicationContext();
104-
ConfigurationPropertySources.attach(this.context.getEnvironment());
105-
this.context.setParent(parent);
106-
this.context.register(JmxAutoConfiguration.class,
107-
IntegrationAutoConfiguration.class);
108-
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context,
109-
"spring.jmx.default_domain=org.foo");
110-
this.context.refresh();
111-
assertThat(this.context.getBean(HeaderChannelRegistry.class)).isNotNull();
86+
this.contextRunner.run((context) -> {
87+
this.contextRunner.withParent(context)
88+
.withPropertyValues("spring.jmx.default_domain=org.foo")
89+
.run((child) -> {
90+
assertThat(child).hasSingleBean(HeaderChannelRegistry.class);
91+
});
92+
});
11293
}
11394

11495
@Test
11596
public void jmxIntegrationEnabledByDefault() {
116-
load();
117-
MBeanServer mBeanServer = this.context.getBean(MBeanServer.class);
118-
assertDomains(mBeanServer, true, "org.springframework.integration",
119-
"org.springframework.integration.monitor");
120-
Object bean = this.context
121-
.getBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
122-
assertThat(bean).isNotNull();
97+
this.contextRunner.run((context) -> {
98+
MBeanServer mBeanServer = context.getBean(MBeanServer.class);
99+
assertThat(mBeanServer.getDomains()).contains(
100+
"org.springframework.integration",
101+
"org.springframework.integration.monitor");
102+
assertThat(context)
103+
.hasBean(IntegrationManagementConfigurer.MANAGEMENT_CONFIGURER_NAME);
104+
});
123105
}
124106

125107
@Test
126108
public void disableJmxIntegration() {
127-
load("spring.jmx.enabled=false");
128-
assertThat(this.context.getBeansOfType(MBeanServer.class)).hasSize(0);
129-
assertThat(this.context.getBeansOfType(IntegrationManagementConfigurer.class))
130-
.isNotEmpty(); // As of Boot 2.0 we always configure
109+
this.contextRunner.withPropertyValues("spring.jmx.enabled=false")
110+
.run((context) -> {
111+
assertThat(context).doesNotHaveBean(MBeanServer.class);
112+
assertThat(context)
113+
.hasSingleBean(IntegrationManagementConfigurer.class);
114+
});
131115
}
132116

133117
@Test
134118
public void customizeJmxDomain() {
135-
load("spring.jmx.default_domain=org.foo");
136-
MBeanServer mBeanServer = this.context.getBean(MBeanServer.class);
137-
assertDomains(mBeanServer, true, "org.foo");
138-
assertDomains(mBeanServer, false, "org.springframework.integration",
139-
"org.springframework.integration.monitor");
119+
this.contextRunner.withPropertyValues("spring.jmx.default_domain=org.foo")
120+
.run((context) -> {
121+
MBeanServer mBeanServer = context.getBean(MBeanServer.class);
122+
assertThat(mBeanServer.getDomains()).contains("org.foo")
123+
.doesNotContain("org.springframework.integration",
124+
"org.springframework.integration.monitor");
125+
});
140126
}
141127

142128
@Test
143129
public void primaryExporterIsAllowed() {
144-
load(new Class[] { CustomMBeanExporter.class });
145-
assertThat(this.context.getBeansOfType(MBeanExporter.class)).hasSize(2);
146-
assertThat(this.context.getBean(MBeanExporter.class))
147-
.isSameAs(this.context.getBean("myMBeanExporter"));
130+
this.contextRunner.withUserConfiguration(CustomMBeanExporter.class)
131+
.run((context) -> {
132+
assertThat(context).getBeans(MBeanExporter.class).hasSize(2);
133+
assertThat(context.getBean(MBeanExporter.class))
134+
.isSameAs(context.getBean("myMBeanExporter"));
135+
});
148136
}
149137

150138
@Test
151139
public void integrationJdbcDataSourceInitializerEnabled() {
152-
load(new Class[] { EmbeddedDataSourceConfiguration.class,
153-
DataSourceTransactionManagerAutoConfiguration.class,
154-
JdbcTemplateAutoConfiguration.class, IntegrationAutoConfiguration.class },
155-
"spring.datasource.generate-unique-name=true",
156-
"spring.integration.jdbc.initialize-schema=always");
157-
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
158-
.getInitializeSchema()).isEqualTo(DataSourceInitializationMode.ALWAYS);
159-
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
160-
assertThat(jdbcOperations.queryForList("select * from INT_MESSAGE")).isEmpty();
161-
assertThat(jdbcOperations.queryForList("select * from INT_GROUP_TO_MESSAGE"))
162-
.isEmpty();
163-
assertThat(jdbcOperations.queryForList("select * from INT_MESSAGE_GROUP"))
164-
.isEmpty();
165-
assertThat(jdbcOperations.queryForList("select * from INT_LOCK")).isEmpty();
166-
assertThat(jdbcOperations.queryForList("select * from INT_CHANNEL_MESSAGE"))
167-
.isEmpty();
140+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
141+
.withConfiguration(AutoConfigurations.of(
142+
DataSourceTransactionManagerAutoConfiguration.class,
143+
JdbcTemplateAutoConfiguration.class,
144+
IntegrationAutoConfiguration.class))
145+
.withPropertyValues("spring.datasource.generate-unique-name=true",
146+
"spring.integration.jdbc.initialize-schema=always")
147+
.run((context) -> {
148+
IntegrationProperties properties = context
149+
.getBean(IntegrationProperties.class);
150+
assertThat(properties.getJdbc().getInitializeSchema())
151+
.isEqualTo(DataSourceInitializationMode.ALWAYS);
152+
JdbcOperations jdbc = context.getBean(JdbcOperations.class);
153+
assertThat(jdbc.queryForList("select * from INT_MESSAGE")).isEmpty();
154+
assertThat(jdbc.queryForList("select * from INT_GROUP_TO_MESSAGE"))
155+
.isEmpty();
156+
assertThat(jdbc.queryForList("select * from INT_MESSAGE_GROUP"))
157+
.isEmpty();
158+
assertThat(jdbc.queryForList("select * from INT_LOCK")).isEmpty();
159+
assertThat(jdbc.queryForList("select * from INT_CHANNEL_MESSAGE"))
160+
.isEmpty();
161+
});
168162
}
169163

170164
@Test
171165
public void integrationJdbcDataSourceInitializerDisabled() {
172-
load(new Class[] { EmbeddedDataSourceConfiguration.class,
173-
DataSourceTransactionManagerAutoConfiguration.class,
174-
JdbcTemplateAutoConfiguration.class, IntegrationAutoConfiguration.class },
175-
"spring.datasource.generate-unique-name=true",
176-
"spring.integration.jdbc.initialize-schema=never");
177-
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
178-
.getInitializeSchema()).isEqualTo(DataSourceInitializationMode.NEVER);
179-
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
180-
this.thrown.expect(BadSqlGrammarException.class);
181-
jdbcOperations.queryForList("select * from INT_MESSAGE");
166+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
167+
.withConfiguration(AutoConfigurations.of(
168+
DataSourceTransactionManagerAutoConfiguration.class,
169+
JdbcTemplateAutoConfiguration.class,
170+
IntegrationAutoConfiguration.class))
171+
.withPropertyValues("spring.datasource.generate-unique-name=true",
172+
"spring.integration.jdbc.initialize-schema=never")
173+
.run((context) -> {
174+
IntegrationProperties properties = context
175+
.getBean(IntegrationProperties.class);
176+
assertThat(properties.getJdbc().getInitializeSchema())
177+
.isEqualTo(DataSourceInitializationMode.NEVER);
178+
JdbcOperations jdbc = context.getBean(JdbcOperations.class);
179+
this.thrown.expect(BadSqlGrammarException.class);
180+
jdbc.queryForList("select * from INT_MESSAGE");
181+
});
182182
}
183183

184184
@Test
185185
public void integrationJdbcDataSourceInitializerEnabledByDefaultWithEmbeddedDb() {
186-
load(new Class[] { EmbeddedDataSourceConfiguration.class,
187-
DataSourceTransactionManagerAutoConfiguration.class,
188-
JdbcTemplateAutoConfiguration.class, IntegrationAutoConfiguration.class },
189-
"spring.datasource.generate-unique-name=true");
190-
assertThat(this.context.getBean(IntegrationProperties.class).getJdbc()
191-
.getInitializeSchema()).isEqualTo(DataSourceInitializationMode.EMBEDDED);
192-
JdbcOperations jdbcOperations = this.context.getBean(JdbcOperations.class);
193-
jdbcOperations.queryForList("select * from INT_MESSAGE").isEmpty();
194-
}
195-
196-
private static void assertDomains(MBeanServer mBeanServer, boolean expected,
197-
String... domains) {
198-
List<String> actual = Arrays.asList(mBeanServer.getDomains());
199-
for (String domain : domains) {
200-
assertThat(actual.contains(domain)).isEqualTo(expected);
201-
}
202-
}
203-
204-
public void load(String... environment) {
205-
load(null, environment);
206-
}
207-
208-
private void load(Class<?>[] configs, String... environment) {
209-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
210-
TestPropertyValues.of(environment).applyTo(ctx);
211-
if (configs != null) {
212-
ctx.register(configs);
213-
}
214-
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(ctx, environment);
215-
ConfigurationPropertySources.attach(ctx.getEnvironment());
216-
ctx.register(JmxAutoConfiguration.class, IntegrationAutoConfiguration.class);
217-
ctx.refresh();
218-
this.context = ctx;
186+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
187+
.withConfiguration(AutoConfigurations.of(
188+
DataSourceTransactionManagerAutoConfiguration.class,
189+
JdbcTemplateAutoConfiguration.class,
190+
IntegrationAutoConfiguration.class))
191+
.withPropertyValues("spring.datasource.generate-unique-name=true")
192+
.run((context) -> {
193+
IntegrationProperties properties = context
194+
.getBean(IntegrationProperties.class);
195+
assertThat(properties.getJdbc().getInitializeSchema())
196+
.isEqualTo(DataSourceInitializationMode.EMBEDDED);
197+
JdbcOperations jdbc = context.getBean(JdbcOperations.class);
198+
jdbc.queryForList("select * from INT_MESSAGE").isEmpty();
199+
});
219200
}
220201

221202
@Configuration

0 commit comments

Comments
 (0)