Skip to content

Commit b51e612

Browse files
committed
Merge pull request #17490 from ayudovin
* pr/17490: Polish "Add test slice for Spring Data Cassandra" Add test slice for Spring Data Cassandra Closes gh-17490
2 parents 7df4918 + 313b2be commit b51e612

21 files changed

+691
-13
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6947,6 +6947,34 @@ TIP: Sometimes writing Spring WebFlux tests is not enough; Spring Boot can help
69476947

69486948

69496949

6950+
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-cassandra-test]]
6951+
==== Auto-configured Data Cassandra Tests
6952+
You can use `@DataCassandraTest` to test Cassandra applications.
6953+
By default, it configures a `CassandraTemplate`, scans for `@Table` classes, and configures Spring Data Cassandra repositories.
6954+
Regular `@Component` beans are not loaded into the `ApplicationContext`.
6955+
(For more about using Cassandra with Spring Boot, see "<<boot-features-cassandra>>", earlier in this chapter.)
6956+
6957+
TIP: A list of the auto-configuration settings that are enabled by `@DataCassandraTest` can be <<appendix-test-auto-configuration.adoc#test-auto-configuration,found in the appendix>>.
6958+
6959+
The following example shows a typical setup for using Cassandra tests in Spring Boot:
6960+
6961+
[source,java,indent=0]
6962+
----
6963+
import org.springframework.beans.factory.annotation.Autowired;
6964+
import org.springframework.boot.test.autoconfigure.data.cassandra.DataCassandraTest;
6965+
6966+
@DataCassandraTest
6967+
class ExampleDataCassandraTests {
6968+
6969+
@Autowired
6970+
private YourRepository repository;
6971+
6972+
//
6973+
}
6974+
----
6975+
6976+
6977+
69506978
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test]]
69516979
==== Auto-configured Data JPA Tests
69526980
You can use the `@DataJpaTest` annotation to test JPA applications.

spring-boot-project/spring-boot-test-autoconfigure/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
optional("org.springframework:spring-web")
3131
optional("org.springframework:spring-webmvc")
3232
optional("org.springframework:spring-webflux")
33+
optional("org.springframework.data:spring-data-cassandra")
3334
optional("org.springframework.data:spring-data-jdbc")
3435
optional("org.springframework.data:spring-data-jpa")
3536
optional("org.springframework.data:spring-data-ldap")
@@ -78,6 +79,7 @@ dependencies {
7879
testImplementation("org.skyscreamer:jsonassert")
7980
testImplementation("org.springframework.hateoas:spring-hateoas")
8081
testImplementation("org.springframework.plugin:spring-plugin-core")
82+
testImplementation("org.testcontainers:cassandra")
8183
testImplementation("org.testcontainers:junit-jupiter")
8284
testImplementation("org.testcontainers:neo4j")
8385
testImplementation("org.testcontainers:testcontainers")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.data.cassandra;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Inherited;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
27+
28+
/**
29+
* {@link ImportAutoConfiguration Auto-configuration imports} for typical Data Cassandra
30+
* tests. Most tests should consider using {@link DataCassandraTest @DataCassandraTest}
31+
* rather than using this annotation directly.
32+
*
33+
* @author Artsiom Yudovin
34+
* @since 2.4.0
35+
* @see DataCassandraTest
36+
*/
37+
@Target(ElementType.TYPE)
38+
@Retention(RetentionPolicy.RUNTIME)
39+
@Documented
40+
@Inherited
41+
@ImportAutoConfiguration
42+
public @interface AutoConfigureDataCassandra {
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.data.cassandra;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Inherited;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
import org.junit.jupiter.api.extension.ExtendWith;
27+
28+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
29+
import org.springframework.boot.autoconfigure.SpringBootApplication;
30+
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
32+
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
33+
import org.springframework.context.annotation.ComponentScan.Filter;
34+
import org.springframework.core.annotation.AliasFor;
35+
import org.springframework.core.env.Environment;
36+
import org.springframework.test.context.BootstrapWith;
37+
import org.springframework.test.context.junit.jupiter.SpringExtension;
38+
39+
/**
40+
* Annotation that can be used for a Cassandra test that focuses <strong>only</strong> on
41+
* Cassandra components.
42+
* <p>
43+
* Using this annotation will disable full auto-configuration and instead apply only
44+
* configuration relevant to Cassandra tests.
45+
* <p>
46+
* When using JUnit 4, this annotation should be used in combination with
47+
* {@code @RunWith(SpringRunner.class)}.
48+
*
49+
* @author Artsiom Yudovin
50+
* @author Stephane Nicoll
51+
* @since 2.4.0
52+
*/
53+
@Target(ElementType.TYPE)
54+
@Retention(RetentionPolicy.RUNTIME)
55+
@Documented
56+
@Inherited
57+
@BootstrapWith(DataCassandraTestContextBootstrapper.class)
58+
@ExtendWith(SpringExtension.class)
59+
@OverrideAutoConfiguration(enabled = false)
60+
@TypeExcludeFilters(DataCassandraTypeExcludeFilter.class)
61+
@AutoConfigureCache
62+
@AutoConfigureDataCassandra
63+
@ImportAutoConfiguration
64+
public @interface DataCassandraTest {
65+
66+
/**
67+
* Properties in form {@literal key=value} that should be added to the Spring
68+
* {@link Environment} before the test runs.
69+
* @return the properties to add
70+
* @since 2.1.0
71+
*/
72+
String[] properties() default {};
73+
74+
/**
75+
* Determines if default filtering should be used with
76+
* {@link SpringBootApplication @SpringBootApplication}. By default no beans are
77+
* included.
78+
* @see #includeFilters()
79+
* @see #excludeFilters()
80+
* @return if default filters should be used
81+
*/
82+
boolean useDefaultFilters() default true;
83+
84+
/**
85+
* A set of include filters which can be used to add otherwise filtered beans to the
86+
* application context.
87+
* @return include filters to apply
88+
*/
89+
Filter[] includeFilters() default {};
90+
91+
/**
92+
* A set of exclude filters which can be used to filter beans that would otherwise be
93+
* added to the application context.
94+
* @return exclude filters to apply
95+
*/
96+
Filter[] excludeFilters() default {};
97+
98+
/**
99+
* Auto-configuration exclusions that should be applied for this test.
100+
* @return auto-configuration exclusions to apply
101+
*/
102+
@AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude")
103+
Class<?>[] excludeAutoConfiguration() default {};
104+
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.data.cassandra;
18+
19+
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20+
import org.springframework.core.annotation.MergedAnnotations;
21+
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
22+
import org.springframework.test.context.TestContextBootstrapper;
23+
24+
/**
25+
* {@link TestContextBootstrapper} for {@link DataCassandraTest @DataCassandraTest}
26+
* support.
27+
*
28+
* @author Artsiom Yudovin
29+
*/
30+
class DataCassandraTestContextBootstrapper extends SpringBootTestContextBootstrapper {
31+
32+
@Override
33+
protected String[] getProperties(Class<?> testClass) {
34+
return MergedAnnotations.from(testClass, SearchStrategy.INHERITED_ANNOTATIONS).get(DataCassandraTest.class)
35+
.getValue("properties", String[].class).orElse(null);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.data.cassandra;
18+
19+
import org.springframework.boot.context.TypeExcludeFilter;
20+
import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter;
21+
22+
/**
23+
* {@link TypeExcludeFilter} for {@link DataCassandraTest @DataCassandraTest}.
24+
*
25+
* @author Artsiom Yudovin
26+
*/
27+
class DataCassandraTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter<DataCassandraTest> {
28+
29+
protected DataCassandraTypeExcludeFilter(Class<?> testClass) {
30+
super(testClass);
31+
}
32+
33+
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
org.springframework.boot.test.autoconfigure.core.AutoConfigureCache=\
33
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration
44

5+
# AutoConfigureDataCassandra auto-configuration imports
6+
org.springframework.boot.test.autoconfigure.data.cassandra.AutoConfigureDataCassandra=\
7+
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
8+
org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\
9+
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveDataAutoConfiguration,\
10+
org.springframework.boot.autoconfigure.data.cassandra.CassandraReactiveRepositoriesAutoConfiguration,\
11+
org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration
12+
513
# AutoConfigureDataJdbc auto-configuration imports
614
org.springframework.boot.test.autoconfigure.data.jdbc.AutoConfigureDataJdbc=\
715
org.springframework.boot.autoconfigure.data.jdbc.JdbcRepositoriesAutoConfiguration,\

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/actuate/metrics/AutoConfigureMetricsSpringBootApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.boot.SpringBootConfiguration;
2020
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2121
import org.springframework.boot.autoconfigure.SpringBootApplication;
22+
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
2223

2324
/**
2425
* Example {@link SpringBootApplication @SpringBootApplication} for use with
@@ -27,7 +28,7 @@
2728
* @author Chris Bono
2829
*/
2930
@SpringBootConfiguration
30-
@EnableAutoConfiguration
31+
@EnableAutoConfiguration(exclude = CassandraAutoConfiguration.class)
3132
class AutoConfigureMetricsSpringBootApplication {
3233

3334
}

0 commit comments

Comments
 (0)