Skip to content

Commit 2916cdf

Browse files
committed
Polish
1 parent 49797b1 commit 2916cdf

File tree

1 file changed

+21
-37
lines changed

1 file changed

+21
-37
lines changed

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.junit.Test;
2525

2626
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
27-
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
2827
import org.springframework.boot.autoconfigure.data.cassandra.city.City;
2928
import org.springframework.boot.autoconfigure.domain.EntityScan;
3029
import org.springframework.boot.test.util.TestPropertyValues;
@@ -39,15 +38,17 @@
3938
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
4039
import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver;
4140
import org.springframework.test.util.ReflectionTestUtils;
41+
import org.springframework.util.ObjectUtils;
4242

4343
import static org.assertj.core.api.Assertions.assertThat;
4444
import static org.mockito.Mockito.mock;
4545

4646
/**
47-
* Tests for {@link CassandraDataAutoConfiguration}
47+
* Tests for {@link CassandraDataAutoConfiguration}.
4848
*
4949
* @author Eddú Meléndez
5050
* @author Mark Paluch
51+
* @author Stephane Nicoll
5152
*/
5253
public class CassandraDataAutoConfigurationTests {
5354

@@ -62,26 +63,15 @@ public void close() {
6263

6364
@Test
6465
public void templateExists() {
65-
this.context = new AnnotationConfigApplicationContext();
66-
TestPropertyValues.of("spring.data.cassandra.keyspaceName:boot_test")
67-
.applyTo(this.context);
68-
this.context.register(TestExcludeConfiguration.class, TestConfiguration.class,
69-
PropertyPlaceholderAutoConfiguration.class,
70-
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class);
71-
this.context.refresh();
66+
load(TestExcludeConfiguration.class);
7267
assertThat(this.context.getBeanNamesForType(CassandraTemplate.class).length)
7368
.isEqualTo(1);
7469
}
7570

7671
@Test
7772
@SuppressWarnings("unchecked")
7873
public void entityScanShouldSetInitialEntitySet() throws Exception {
79-
this.context = new AnnotationConfigApplicationContext();
80-
TestPropertyValues.of("spring.data.cassandra.keyspaceName:boot_test");
81-
this.context.register(TestConfiguration.class, EntityScanConfig.class,
82-
PropertyPlaceholderAutoConfiguration.class,
83-
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class);
84-
this.context.refresh();
74+
load(EntityScanConfig.class);
8575
CassandraMappingContext mappingContext = this.context
8676
.getBean(CassandraMappingContext.class);
8777
Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils
@@ -91,13 +81,7 @@ public void entityScanShouldSetInitialEntitySet() throws Exception {
9181

9282
@Test
9383
public void userTypeResolverShouldBeSet() throws Exception {
94-
this.context = new AnnotationConfigApplicationContext();
95-
TestPropertyValues.of("spring.data.cassandra.keyspaceName:boot_test")
96-
.applyTo(this.context);
97-
this.context.register(TestConfiguration.class,
98-
PropertyPlaceholderAutoConfiguration.class,
99-
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class);
100-
this.context.refresh();
84+
load();
10185
CassandraMappingContext mappingContext = this.context
10286
.getBean(CassandraMappingContext.class);
10387
assertThat(ReflectionTestUtils.getField(mappingContext, "userTypeResolver"))
@@ -106,34 +90,34 @@ public void userTypeResolverShouldBeSet() throws Exception {
10690

10791
@Test
10892
public void defaultConversions() {
109-
this.context = new AnnotationConfigApplicationContext();
110-
TestPropertyValues.of("spring.data.cassandra.keyspaceName:boot_test")
111-
.applyTo(this.context);
112-
this.context.register(TestConfiguration.class,
113-
PropertyPlaceholderAutoConfiguration.class,
114-
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class);
115-
this.context.refresh();
93+
load();
11694
CassandraTemplate template = this.context.getBean(CassandraTemplate.class);
11795
assertThat(template.getConverter().getConversionService().canConvert(Person.class,
11896
String.class)).isFalse();
11997
}
12098

12199
@Test
122100
public void customConversions() {
123-
this.context = new AnnotationConfigApplicationContext();
124-
TestPropertyValues.of("spring.data.cassandra.keyspaceName:boot_test")
125-
.applyTo(this.context);
126-
this.context.register(CustomConversionConfig.class,
127-
TestConfiguration.class,
128-
PropertyPlaceholderAutoConfiguration.class,
129-
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class);
130-
this.context.refresh();
101+
load(CustomConversionConfig.class);
131102
CassandraTemplate template = this.context.getBean(CassandraTemplate.class);
132103
assertThat(template.getConverter().getConversionService().canConvert(Person.class,
133104
String.class)).isTrue();
134105

135106
}
136107

108+
public void load(Class<?>... config) {
109+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
110+
TestPropertyValues.of("spring.data.cassandra.keyspaceName:boot_test")
111+
.applyTo(ctx);
112+
if (!ObjectUtils.isEmpty(config)) {
113+
ctx.register(config);
114+
}
115+
ctx.register(TestConfiguration.class, CassandraAutoConfiguration.class,
116+
CassandraDataAutoConfiguration.class);
117+
ctx.refresh();
118+
this.context = ctx;
119+
}
120+
137121
@Configuration
138122
@ComponentScan(excludeFilters = @ComponentScan.Filter(classes = {
139123
Session.class }, type = FilterType.ASSIGNABLE_TYPE))

0 commit comments

Comments
 (0)