Switch TransactionAutoConfigurationTests to ApplicationContextRunner

pull/18295/head
Andy Wilkinson 5 years ago
parent 070eedaa19
commit 3b01bc0366

@ -16,24 +16,21 @@
package org.springframework.boot.autoconfigure.transaction; package org.springframework.boot.autoconfigure.transaction;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 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;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -51,98 +48,83 @@ import static org.mockito.Mockito.mock;
*/ */
class TransactionAutoConfigurationTests { class TransactionAutoConfigurationTests {
private AnnotationConfigApplicationContext context; private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(TransactionAutoConfiguration.class));
@AfterEach
void tearDown() {
if (this.context != null) {
this.context.close();
}
}
@Test @Test
void noTransactionManager() { void noTransactionManager() {
load(EmptyConfiguration.class); this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(TransactionTemplate.class));
assertThat(this.context.getBeansOfType(TransactionTemplate.class)).isEmpty();
} }
@Test @Test
void singleTransactionManager() { void singleTransactionManager() {
load(new Class<?>[] { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class }, this.contextRunner
"spring.datasource.initialization-mode:never"); .withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class,
PlatformTransactionManager transactionManager = this.context.getBean(PlatformTransactionManager.class); DataSourceTransactionManagerAutoConfiguration.class))
TransactionTemplate transactionTemplate = this.context.getBean(TransactionTemplate.class); .withPropertyValues("spring.datasource.initialization-mode:never").run((context) -> {
assertThat(transactionTemplate.getTransactionManager()).isSameAs(transactionManager); PlatformTransactionManager transactionManager = context.getBean(PlatformTransactionManager.class);
TransactionTemplate transactionTemplate = context.getBean(TransactionTemplate.class);
assertThat(transactionTemplate.getTransactionManager()).isSameAs(transactionManager);
});
} }
@Test @Test
void severalTransactionManagers() { void severalTransactionManagers() {
load(SeveralTransactionManagersConfiguration.class); this.contextRunner.withUserConfiguration(SeveralTransactionManagersConfiguration.class)
assertThat(this.context.getBeansOfType(TransactionTemplate.class)).isEmpty(); .run((context) -> assertThat(context).doesNotHaveBean(TransactionTemplate.class));
} }
@Test @Test
void customTransactionManager() { void customTransactionManager() {
load(CustomTransactionManagerConfiguration.class); this.contextRunner.withUserConfiguration(CustomTransactionManagerConfiguration.class).run((context) -> {
Map<String, TransactionTemplate> beans = this.context.getBeansOfType(TransactionTemplate.class); assertThat(context).hasSingleBean(TransactionTemplate.class);
assertThat(beans).hasSize(1); assertThat(context.getBean("transactionTemplateFoo")).isInstanceOf(TransactionTemplate.class);
assertThat(beans.containsKey("transactionTemplateFoo")).isTrue(); });
} }
@Test @Test
void platformTransactionManagerCustomizers() { void platformTransactionManagerCustomizers() {
load(SeveralTransactionManagersConfiguration.class); this.contextRunner.withUserConfiguration(SeveralTransactionManagersConfiguration.class).run((context) -> {
TransactionManagerCustomizers customizers = this.context.getBean(TransactionManagerCustomizers.class); TransactionManagerCustomizers customizers = context.getBean(TransactionManagerCustomizers.class);
List<?> field = (List<?>) ReflectionTestUtils.getField(customizers, "customizers"); assertThat(customizers).extracting("customizers").asList().hasSize(1).first()
assertThat(field).hasSize(1).first().isInstanceOf(TransactionProperties.class); .isInstanceOf(TransactionProperties.class);
});
} }
@Test @Test
void transactionNotManagedWithNoTransactionManager() { void transactionNotManagedWithNoTransactionManager() {
load(BaseConfiguration.class); this.contextRunner.withUserConfiguration(BaseConfiguration.class).run(
assertThat(this.context.getBean(TransactionalService.class).isTransactionActive()).isFalse(); (context) -> assertThat(context.getBean(TransactionalService.class).isTransactionActive()).isFalse());
} }
@Test @Test
void transactionManagerUsesCglibByDefault() { void transactionManagerUsesCglibByDefault() {
load(TransactionManagersConfiguration.class); this.contextRunner.withUserConfiguration(TransactionManagersConfiguration.class).run((context) -> {
assertThat(this.context.getBean(AnotherServiceImpl.class).isTransactionActive()).isTrue(); assertThat(context.getBean(AnotherServiceImpl.class).isTransactionActive()).isTrue();
assertThat(this.context.getBeansOfType(TransactionalServiceImpl.class)).hasSize(1); assertThat(context.getBeansOfType(TransactionalServiceImpl.class)).hasSize(1);
});
} }
@Test @Test
void transactionManagerCanBeConfiguredToJdkProxy() { void transactionManagerCanBeConfiguredToJdkProxy() {
load(TransactionManagersConfiguration.class, "spring.aop.proxy-target-class=false"); this.contextRunner.withUserConfiguration(TransactionManagersConfiguration.class)
assertThat(this.context.getBean(AnotherService.class).isTransactionActive()).isTrue(); .withPropertyValues("spring.aop.proxy-target-class=false").run((context) -> {
assertThat(this.context.getBeansOfType(AnotherServiceImpl.class)).hasSize(0); assertThat(context.getBean(AnotherService.class).isTransactionActive()).isTrue();
assertThat(this.context.getBeansOfType(TransactionalServiceImpl.class)).hasSize(0); assertThat(context).doesNotHaveBean(AnotherServiceImpl.class);
assertThat(context).doesNotHaveBean(TransactionalServiceImpl.class);
});
} }
@Test @Test
void customEnableTransactionManagementTakesPrecedence() { void customEnableTransactionManagementTakesPrecedence() {
load(new Class<?>[] { CustomTransactionManagementConfiguration.class, TransactionManagersConfiguration.class }, this.contextRunner
"spring.aop.proxy-target-class=true"); .withUserConfiguration(CustomTransactionManagementConfiguration.class,
assertThat(this.context.getBean(AnotherService.class).isTransactionActive()).isTrue(); TransactionManagersConfiguration.class)
assertThat(this.context.getBeansOfType(AnotherServiceImpl.class)).hasSize(0); .withPropertyValues("spring.aop.proxy-target-class=true").run((context) -> {
assertThat(this.context.getBeansOfType(TransactionalServiceImpl.class)).hasSize(0); assertThat(context.getBean(AnotherService.class).isTransactionActive()).isTrue();
} assertThat(context).doesNotHaveBean(AnotherServiceImpl.class);
assertThat(context).doesNotHaveBean(TransactionalServiceImpl.class);
private void load(Class<?> config, String... environment) { });
load(new Class<?>[] { config }, environment);
}
private void load(Class<?>[] configs, String... environment) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(configs);
applicationContext.register(TransactionAutoConfiguration.class);
TestPropertyValues.of(environment).applyTo(applicationContext);
applicationContext.refresh();
this.context = applicationContext;
}
@Configuration(proxyBeanMethods = false)
static class EmptyConfiguration {
} }
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)

Loading…
Cancel
Save