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;
import org.junit.After;
import org.junit.Test;
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.ModifiedClassPathRunner;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -40,18 +39,12 @@ import static org.assertj.core.api.Assertions.assertThat;
@ClassPathExclusions("spring-context-support-*.jar")
public class ConditionalOnMissingBeanWithFilteredClasspathTests {
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@After
public void closeContext() {
this.context.close();
}
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withUserConfiguration(OnBeanTypeConfiguration.class);
@Test
public void testNameOnMissingBeanTypeWithMissingImport() {
this.context.register(OnBeanTypeConfiguration.class);
this.context.refresh();
assertThat(this.context.containsBean("foo")).isTrue();
this.contextRunner.run((context) -> assertThat(context).hasBean("foo"));
}
@Configuration(proxyBeanMethods = false)

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

@ -18,13 +18,13 @@ package org.springframework.boot.autoconfigure.validation;
import javax.validation.Validator;
import org.junit.After;
import org.junit.Test;
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.ModifiedClassPathRunner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
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" })
public class ValidationAutoConfigurationWithHibernateValidatorMissingElImplTests {
private AnnotationConfigApplicationContext context;
@After
public void close() {
if (this.context != null) {
this.context.close();
}
}
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(ValidationAutoConfiguration.class));
@Test
public void missingElDependencyIsTolerated() {
this.context = new AnnotationConfigApplicationContext(ValidationAutoConfiguration.class);
assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1);
assertThat(this.context.getBeansOfType(MethodValidationPostProcessor.class)).hasSize(1);
this.contextRunner.run((context) -> {
assertThat(context).hasSingleBean(Validator.class);
assertThat(context).hasSingleBean(MethodValidationPostProcessor.class);
});
}
}

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

Loading…
Cancel
Save