Migrate to ApplicationContextRunner

Migrate a new tests to use the `ApplicationContextRunner`.

See gh-17491
pull/17516/head
dreis2211 5 years ago committed by Phillip Webb
parent 0cb6a7f47d
commit b0bf2dd7ab

@ -16,14 +16,13 @@
package org.springframework.boot.autoconfigure.condition; package org.springframework.boot.autoconfigure.condition;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.cache.caffeine.CaffeineCacheManager; import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -40,18 +39,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@ClassPathExclusions("spring-context-support-*.jar") @ClassPathExclusions("spring-context-support-*.jar")
public class ConditionalOnMissingBeanWithFilteredClasspathTests { public class ConditionalOnMissingBeanWithFilteredClasspathTests {
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(OnBeanTypeConfiguration.class);
@After
public void closeContext() {
this.context.close();
}
@Test @Test
public void testNameOnMissingBeanTypeWithMissingImport() { public void testNameOnMissingBeanTypeWithMissingImport() {
this.context.register(OnBeanTypeConfiguration.class); this.contextRunner.run((context) -> assertThat(context).hasBean("foo"));
this.context.refresh();
assertThat(this.context.containsBean("foo")).isTrue();
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

@ -16,13 +16,13 @@
package org.springframework.boot.autoconfigure.http; package org.springframework.boot.autoconfigure.http;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -36,20 +36,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@ClassPathExclusions("jackson-*.jar") @ClassPathExclusions("jackson-*.jar")
public class HttpMessageConvertersAutoConfigurationWithoutJacksonTests { public class HttpMessageConvertersAutoConfigurationWithoutJacksonTests {
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(HttpMessageConvertersAutoConfiguration.class));
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void autoConfigurationWorksWithSpringHateoasButWithoutJackson() { public void autoConfigurationWorksWithSpringHateoasButWithoutJackson() {
this.context.register(HttpMessageConvertersAutoConfiguration.class); this.contextRunner.run((context) -> assertThat(context).hasSingleBean(HttpMessageConverters.class));
this.context.refresh();
assertThat(this.context.getBeansOfType(HttpMessageConverters.class)).hasSize(1);
} }
} }

@ -18,13 +18,13 @@ package org.springframework.boot.autoconfigure.validation;
import javax.validation.Validator; import javax.validation.Validator;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -39,20 +39,15 @@ import static org.assertj.core.api.Assertions.assertThat;
@ClassPathExclusions({ "tomcat-embed-el-*.jar", "el-api-*.jar" }) @ClassPathExclusions({ "tomcat-embed-el-*.jar", "el-api-*.jar" })
public class ValidationAutoConfigurationWithHibernateValidatorMissingElImplTests { public class ValidationAutoConfigurationWithHibernateValidatorMissingElImplTests {
private AnnotationConfigApplicationContext context; private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ValidationAutoConfiguration.class));
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void missingElDependencyIsTolerated() { public void missingElDependencyIsTolerated() {
this.context = new AnnotationConfigApplicationContext(ValidationAutoConfiguration.class); this.contextRunner.run((context) -> {
assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1); assertThat(context).hasSingleBean(Validator.class);
assertThat(this.context.getBeansOfType(MethodValidationPostProcessor.class)).hasSize(1); assertThat(context).hasSingleBean(MethodValidationPostProcessor.class);
});
} }
} }

@ -18,13 +18,13 @@ package org.springframework.boot.autoconfigure.validation;
import javax.validation.Validator; import javax.validation.Validator;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -38,20 +38,15 @@ import static org.assertj.core.api.Assertions.assertThat;
@ClassPathExclusions("hibernate-validator-*.jar") @ClassPathExclusions("hibernate-validator-*.jar")
public class ValidationAutoConfigurationWithoutValidatorTests { public class ValidationAutoConfigurationWithoutValidatorTests {
private AnnotationConfigApplicationContext context; private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ValidationAutoConfiguration.class));
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void validationIsDisabled() { public void validationIsDisabled() {
this.context = new AnnotationConfigApplicationContext(ValidationAutoConfiguration.class); this.contextRunner.run((context) -> {
assertThat(this.context.getBeansOfType(Validator.class)).isEmpty(); assertThat(context).doesNotHaveBean(Validator.class);
assertThat(this.context.getBeansOfType(MethodValidationPostProcessor.class)).isEmpty(); assertThat(context).doesNotHaveBean(MethodValidationPostProcessor.class);
});
} }
} }

Loading…
Cancel
Save