Migrate test to WebApplicationContextTester

pull/9855/merge
Stephane Nicoll 7 years ago
parent 4c64f4f80d
commit 9891462ac4

@ -16,7 +16,6 @@
package org.springframework.boot.autoconfigure.data.neo4j; package org.springframework.boot.autoconfigure.data.neo4j;
import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.neo4j.ogm.session.Session; import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory; import org.neo4j.ogm.session.SessionFactory;
@ -24,22 +23,19 @@ import org.neo4j.ogm.session.event.Event;
import org.neo4j.ogm.session.event.EventListener; import org.neo4j.ogm.session.event.EventListener;
import org.neo4j.ogm.session.event.PersistenceEvent; import org.neo4j.ogm.session.event.PersistenceEvent;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages; import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.data.neo4j.city.City; import org.springframework.boot.autoconfigure.data.neo4j.city.City;
import org.springframework.boot.autoconfigure.data.neo4j.country.Country; import org.springframework.boot.autoconfigure.data.neo4j.country.Country;
import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration; import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.context.WebApplicationContextTester;
import org.springframework.context.ConfigurableApplicationContext;
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.data.neo4j.mapping.Neo4jMappingContext; import org.springframework.data.neo4j.mapping.Neo4jMappingContext;
import org.springframework.data.neo4j.transaction.Neo4jTransactionManager; import org.springframework.data.neo4j.transaction.Neo4jTransactionManager;
import org.springframework.data.neo4j.web.support.OpenSessionInViewInterceptor; import org.springframework.data.neo4j.web.support.OpenSessionInViewInterceptor;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
@ -58,96 +54,88 @@ import static org.mockito.Mockito.verify;
*/ */
public class Neo4jDataAutoConfigurationTests { public class Neo4jDataAutoConfigurationTests {
private ConfigurableApplicationContext context; private WebApplicationContextTester context = new WebApplicationContextTester()
.withUserConfiguration(TestConfiguration.class)
@After .withConfiguration(AutoConfigurations.of(Neo4jDataAutoConfiguration.class,
public void close() { TransactionAutoConfiguration.class));
if (this.context != null) {
this.context.close();
}
}
@Test @Test
public void defaultConfiguration() { public void defaultConfiguration() {
load(null, "spring.data.neo4j.uri=http://localhost:8989"); this.context.withPropertyValues("spring.data.neo4j.uri=http://localhost:8989")
assertThat(this.context.getBeansOfType(org.neo4j.ogm.config.Configuration.class)) .run((loaded) -> {
.hasSize(1); assertThat(loaded).hasSingleBean(
assertThat(this.context.getBeansOfType(SessionFactory.class)).hasSize(1); org.neo4j.ogm.config.Configuration.class);
assertThat(this.context.getBeansOfType(Neo4jTransactionManager.class)).hasSize(1); assertThat(loaded).hasSingleBean(SessionFactory.class);
assertThat(this.context.getBeansOfType(OpenSessionInViewInterceptor.class)) assertThat(loaded).hasSingleBean(Neo4jTransactionManager.class);
.hasSize(1); assertThat(loaded).hasSingleBean(OpenSessionInViewInterceptor.class);
});
} }
@Test @Test
public void customNeo4jTransactionManagerUsingProperties() { public void customNeo4jTransactionManagerUsingProperties() {
load(null, "spring.transaction.default-timeout=30", this.context.withPropertyValues("spring.transaction.default-timeout=30",
"spring.transaction.rollback-on-commit-failure:true"); "spring.transaction.rollback-on-commit-failure:true").run((loaded) -> {
Neo4jTransactionManager transactionManager = this.context Neo4jTransactionManager transactionManager = loaded
.getBean(Neo4jTransactionManager.class); .getBean(Neo4jTransactionManager.class);
assertThat(transactionManager.getDefaultTimeout()).isEqualTo(30); assertThat(transactionManager.getDefaultTimeout()).isEqualTo(30);
assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue(); assertThat(transactionManager.isRollbackOnCommitFailure()).isTrue();
});
} }
@Test @Test
public void customSessionFactory() { public void customSessionFactory() {
load(CustomSessionFactory.class); this.context.withUserConfiguration(CustomSessionFactory.class).run((loaded) -> {
assertThat(this.context.getBeansOfType(org.neo4j.ogm.config.Configuration.class)) assertThat(loaded).doesNotHaveBean(org.neo4j.ogm.config.Configuration.class);
.hasSize(0); assertThat(loaded).hasSingleBean(SessionFactory.class);
assertThat(this.context.getBeansOfType(SessionFactory.class)).hasSize(1); });
} }
@Test @Test
public void customConfiguration() { public void customConfiguration() {
load(CustomConfiguration.class); this.context.withUserConfiguration(CustomConfiguration.class).run((loaded) -> {
assertThat(this.context.getBean(org.neo4j.ogm.config.Configuration.class)) assertThat(loaded.getBean(org.neo4j.ogm.config.Configuration.class))
.isSameAs(this.context.getBean("myConfiguration")); .isSameAs(loaded.getBean("myConfiguration"));
assertThat(this.context.getBeansOfType(SessionFactory.class)).hasSize(1); assertThat(loaded).hasSingleBean(SessionFactory.class);
assertThat(this.context.getBeansOfType(org.neo4j.ogm.config.Configuration.class)) assertThat(loaded).hasSingleBean(org.neo4j.ogm.config.Configuration.class);
.hasSize(1); });
} }
@Test @Test
public void usesAutoConfigurationPackageToPickUpDomainTypes() { public void usesAutoConfigurationPackageToPickUpDomainTypes() {
this.context = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
String cityPackage = City.class.getPackage().getName(); String cityPackage = City.class.getPackage().getName();
AutoConfigurationPackages.register((BeanDefinitionRegistry) this.context, AutoConfigurationPackages.register(context, cityPackage);
cityPackage); context.register(Neo4jDataAutoConfiguration.class,
((AnnotationConfigApplicationContext) this.context).register(
Neo4jDataAutoConfiguration.class,
Neo4jRepositoriesAutoConfiguration.class); Neo4jRepositoriesAutoConfiguration.class);
this.context.refresh(); try {
assertDomainTypesDiscovered(this.context.getBean(Neo4jMappingContext.class), context.refresh();
City.class); assertDomainTypesDiscovered(context.getBean(Neo4jMappingContext.class),
City.class);
}
finally {
context.close();
}
} }
@Test @Test
public void openSessionInViewInterceptorCanBeDisabled() { public void openSessionInViewInterceptorCanBeDisabled() {
load(null, "spring.data.neo4j.open-in-view:false"); this.context.withPropertyValues("spring.data.neo4j.open-in-view:false")
assertThat(this.context.getBeansOfType(OpenSessionInViewInterceptor.class)) .run((loaded) -> assertThat(loaded).doesNotHaveBean(
.isEmpty(); OpenSessionInViewInterceptor.class));
} }
@Test @Test
public void eventListenersAreAutoRegistered() { public void eventListenersAreAutoRegistered() {
load(EventListenerConfiguration.class); this.context.withUserConfiguration(EventListenerConfiguration.class)
Session session = this.context.getBean(SessionFactory.class).openSession(); .run((loaded) -> {
session.notifyListeners(new PersistenceEvent(null, Event.TYPE.PRE_SAVE)); Session session = loaded.getBean(SessionFactory.class).openSession();
verify(this.context.getBean("eventListenerOne", EventListener.class)) session.notifyListeners(new PersistenceEvent(null, Event.TYPE.PRE_SAVE));
.onPreSave(any(Event.class)); verify(loaded.getBean("eventListenerOne", EventListener.class))
verify(this.context.getBean("eventListenerTwo", EventListener.class)) .onPreSave(any(Event.class));
.onPreSave(any(Event.class)); verify(loaded.getBean("eventListenerTwo", EventListener.class))
} .onPreSave(any(Event.class));
});
private void load(Class<?> config, String... environment) {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
TestPropertyValues.of(environment).applyTo(ctx);
if (config != null) {
ctx.register(config);
}
ctx.register(TestConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
Neo4jDataAutoConfiguration.class, TransactionAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
} }
private static void assertDomainTypesDiscovered(Neo4jMappingContext mappingContext, private static void assertDomainTypesDiscovered(Neo4jMappingContext mappingContext,

Loading…
Cancel
Save