File tree Expand file tree Collapse file tree 4 files changed +25
-50
lines changed
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure Expand file tree Collapse file tree 4 files changed +25
-50
lines changed Original file line number Diff line number Diff line change 1616
1717package org .springframework .boot .autoconfigure .condition ;
1818
19- import org .junit .After ;
2019import org .junit .Test ;
2120import org .junit .runner .RunWith ;
2221
22+ import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
2323import org .springframework .boot .testsupport .runner .classpath .ClassPathExclusions ;
2424import org .springframework .boot .testsupport .runner .classpath .ModifiedClassPathRunner ;
2525import org .springframework .cache .caffeine .CaffeineCacheManager ;
26- import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
2726import org .springframework .context .annotation .Bean ;
2827import org .springframework .context .annotation .Configuration ;
2928
4039@ ClassPathExclusions ("spring-context-support-*.jar" )
4140public class ConditionalOnMissingBeanWithFilteredClasspathTests {
4241
43- private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
44-
45- @ After
46- public void closeContext () {
47- this .context .close ();
48- }
42+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
43+ .withUserConfiguration (OnBeanTypeConfiguration .class );
4944
5045 @ Test
5146 public void testNameOnMissingBeanTypeWithMissingImport () {
52- this .context .register (OnBeanTypeConfiguration .class );
53- this .context .refresh ();
54- assertThat (this .context .containsBean ("foo" )).isTrue ();
47+ this .contextRunner .run ((context ) -> assertThat (context ).hasBean ("foo" ));
5548 }
5649
5750 @ Configuration (proxyBeanMethods = false )
Original file line number Diff line number Diff line change 1616
1717package org .springframework .boot .autoconfigure .http ;
1818
19- import org .junit .After ;
2019import org .junit .Test ;
2120import org .junit .runner .RunWith ;
2221
22+ import org .springframework .boot .autoconfigure .AutoConfigurations ;
23+ import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
2324import org .springframework .boot .testsupport .runner .classpath .ClassPathExclusions ;
2425import org .springframework .boot .testsupport .runner .classpath .ModifiedClassPathRunner ;
25- import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
2626
2727import static org .assertj .core .api .Assertions .assertThat ;
2828
3636@ ClassPathExclusions ("jackson-*.jar" )
3737public class HttpMessageConvertersAutoConfigurationWithoutJacksonTests {
3838
39- private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
40-
41- @ After
42- public void close () {
43- if (this .context != null ) {
44- this .context .close ();
45- }
46- }
39+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
40+ .withConfiguration (AutoConfigurations .of (HttpMessageConvertersAutoConfiguration .class ));
4741
4842 @ Test
4943 public void autoConfigurationWorksWithSpringHateoasButWithoutJackson () {
50- this .context .register (HttpMessageConvertersAutoConfiguration .class );
51- this .context .refresh ();
52- assertThat (this .context .getBeansOfType (HttpMessageConverters .class )).hasSize (1 );
44+ this .contextRunner .run ((context ) -> assertThat (context ).hasSingleBean (HttpMessageConverters .class ));
5345 }
5446
5547}
Original file line number Diff line number Diff line change 1818
1919import javax .validation .Validator ;
2020
21- import org .junit .After ;
2221import org .junit .Test ;
2322import org .junit .runner .RunWith ;
2423
24+ import org .springframework .boot .autoconfigure .AutoConfigurations ;
25+ import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
2526import org .springframework .boot .testsupport .runner .classpath .ClassPathExclusions ;
2627import org .springframework .boot .testsupport .runner .classpath .ModifiedClassPathRunner ;
27- import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
2828import org .springframework .validation .beanvalidation .MethodValidationPostProcessor ;
2929
3030import static org .assertj .core .api .Assertions .assertThat ;
3939@ ClassPathExclusions ({ "tomcat-embed-el-*.jar" , "el-api-*.jar" })
4040public class ValidationAutoConfigurationWithHibernateValidatorMissingElImplTests {
4141
42- private AnnotationConfigApplicationContext context ;
43-
44- @ After
45- public void close () {
46- if (this .context != null ) {
47- this .context .close ();
48- }
49- }
42+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
43+ .withConfiguration (AutoConfigurations .of (ValidationAutoConfiguration .class ));
5044
5145 @ Test
5246 public void missingElDependencyIsTolerated () {
53- this .context = new AnnotationConfigApplicationContext (ValidationAutoConfiguration .class );
54- assertThat (this .context .getBeansOfType (Validator .class )).hasSize (1 );
55- assertThat (this .context .getBeansOfType (MethodValidationPostProcessor .class )).hasSize (1 );
47+ this .contextRunner .run ((context ) -> {
48+ assertThat (context ).hasSingleBean (Validator .class );
49+ assertThat (context ).hasSingleBean (MethodValidationPostProcessor .class );
50+ });
5651 }
5752
5853}
Original file line number Diff line number Diff line change 1818
1919import javax .validation .Validator ;
2020
21- import org .junit .After ;
2221import org .junit .Test ;
2322import org .junit .runner .RunWith ;
2423
24+ import org .springframework .boot .autoconfigure .AutoConfigurations ;
25+ import org .springframework .boot .test .context .runner .ApplicationContextRunner ;
2526import org .springframework .boot .testsupport .runner .classpath .ClassPathExclusions ;
2627import org .springframework .boot .testsupport .runner .classpath .ModifiedClassPathRunner ;
27- import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
2828import org .springframework .validation .beanvalidation .MethodValidationPostProcessor ;
2929
3030import static org .assertj .core .api .Assertions .assertThat ;
3838@ ClassPathExclusions ("hibernate-validator-*.jar" )
3939public class ValidationAutoConfigurationWithoutValidatorTests {
4040
41- private AnnotationConfigApplicationContext context ;
42-
43- @ After
44- public void close () {
45- if (this .context != null ) {
46- this .context .close ();
47- }
48- }
41+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
42+ .withConfiguration (AutoConfigurations .of (ValidationAutoConfiguration .class ));
4943
5044 @ Test
5145 public void validationIsDisabled () {
52- this .context = new AnnotationConfigApplicationContext (ValidationAutoConfiguration .class );
53- assertThat (this .context .getBeansOfType (Validator .class )).isEmpty ();
54- assertThat (this .context .getBeansOfType (MethodValidationPostProcessor .class )).isEmpty ();
46+ this .contextRunner .run ((context ) -> {
47+ assertThat (context ).doesNotHaveBean (Validator .class );
48+ assertThat (context ).doesNotHaveBean (MethodValidationPostProcessor .class );
49+ });
5550 }
5651
5752}
You can’t perform that action at this time.
0 commit comments