Skip to content

Commit 68fed47

Browse files
committed
Merge pull request #29912 from eddumelendez
* gh-29912: Polish "Add DataCouchbaseTest annotation" Add DataCouchbaseTest annotation Closes gh-29912
2 parents 8015fb0 + 664ecbd commit 68fed47

File tree

21 files changed

+823
-0
lines changed

21 files changed

+823
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,22 @@ include::code:MyDataCassandraTests[]
472472

473473

474474

475+
[[features.testing.spring-boot-applications.autoconfigured-spring-data-couchbase]]
476+
==== Auto-configured Data Couchbase Tests
477+
You can use `@DataCouchbaseTest` to test Couchbase applications.
478+
By default, it configures a `CouchbaseTemplate` or `ReactiveCouchbaseTemplate`, scans for `@Document` classes, and configures Spring Data Couchbase repositories.
479+
Regular `@Component` and `@ConfigurationProperties` beans are not scanned when the `@DataCouchbaseTest` annotation is used.
480+
`@EnableConfigurationProperties` can be used to include `@ConfigurationProperties` beans.
481+
(For more about using Couchbase with Spring Boot, see "<<data#data.nosql.couchbase>>", earlier in this chapter.)
482+
483+
TIP: A list of the auto-configuration settings that are enabled by `@DataCouchbaseTest` can be <<test-auto-configuration#appendix.test-auto-configuration,found in the appendix>>.
484+
485+
The following example shows a typical setup for using Couchbase tests in Spring Boot:
486+
487+
include::code:MyDataCouchbaseTests[]
488+
489+
490+
475491
[[features.testing.spring-boot-applications.autoconfigured-spring-data-elasticsearch]]
476492
==== Auto-configured Data Elasticsearch Tests
477493
You can use `@DataElasticsearchTest` to test Elasticsearch applications.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2021 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.docs.features.testing.springbootapplications.autoconfiguredspringdatacouchbase;
18+
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.boot.test.autoconfigure.data.couchbase.DataCouchbaseTest;
21+
22+
@DataCouchbaseTest
23+
class MyDataCouchbaseTests {
24+
25+
@Autowired
26+
@SuppressWarnings("unused")
27+
private SomeRepository repository;
28+
29+
// ...
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2012-2021 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.docs.features.testing.springbootapplications.autoconfiguredspringdatacouchbase;
18+
19+
interface SomeRepository {
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2012-2022 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.docs.features.testing.springbootapplications.autoconfiguredspringdatacouchbase
18+
19+
import org.springframework.beans.factory.annotation.Autowired
20+
import org.springframework.boot.test.autoconfigure.data.couchbase.DataCouchbaseTest
21+
22+
@DataCouchbaseTest
23+
class MyDataCouchbaseTests(@Autowired val repository: SomeRepository) {
24+
25+
// ...
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2012-2022 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.docs.features.testing.springbootapplications.autoconfiguredspringdatacouchbase
18+
19+
interface SomeRepository

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ dependencies {
4848
optional("org.springframework.data:spring-data-cassandra") {
4949
exclude group: "org.slf4j", module: "jcl-over-slf4j"
5050
}
51+
optional("org.springframework.data:spring-data-couchbase")
5152
optional("org.springframework.data:spring-data-elasticsearch")
5253
optional("org.springframework.data:spring-data-jdbc")
5354
optional("org.springframework.data:spring-data-jpa")
@@ -109,6 +110,7 @@ dependencies {
109110
testImplementation("org.springframework.hateoas:spring-hateoas")
110111
testImplementation("org.springframework.plugin:spring-plugin-core")
111112
testImplementation("org.testcontainers:cassandra")
113+
testImplementation("org.testcontainers:couchbase")
112114
testImplementation("org.testcontainers:elasticsearch")
113115
testImplementation("org.testcontainers:junit-jupiter")
114116
testImplementation("org.testcontainers:mongodb")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2022 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.couchbase;
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 Couchbase
30+
* tests. Most tests should consider using {@link DataCouchbaseTest @DataCouchbaseTest}
31+
* rather than using this annotation directly.
32+
*
33+
* @author Eddú Meléndez
34+
* @since 2.7.0
35+
* @see DataCouchbaseTest
36+
*/
37+
@Target(ElementType.TYPE)
38+
@Retention(RetentionPolicy.RUNTIME)
39+
@Documented
40+
@Inherited
41+
@ImportAutoConfiguration
42+
public @interface AutoConfigureDataCouchbase {
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2012-2022 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.couchbase;
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 Data Couchbase test that focuses
41+
* <strong>only</strong> on Data Couchbase components.
42+
* <p>
43+
* Using this annotation will disable full auto-configuration and instead apply only
44+
* configuration relevant to Data Couchbase tests.
45+
* <p>
46+
* When using JUnit 4, this annotation should be used in combination with
47+
* {@code @RunWith(SpringRunner.class)}.
48+
*
49+
* @author Eddú Meléndez
50+
* @since 2.7.0
51+
*/
52+
@Target(ElementType.TYPE)
53+
@Retention(RetentionPolicy.RUNTIME)
54+
@Documented
55+
@Inherited
56+
@BootstrapWith(DataCouchbaseTestContextBootstrapper.class)
57+
@ExtendWith(SpringExtension.class)
58+
@OverrideAutoConfiguration(enabled = false)
59+
@TypeExcludeFilters(DataCouchbaseTypeExcludeFilter.class)
60+
@AutoConfigureCache
61+
@AutoConfigureDataCouchbase
62+
@ImportAutoConfiguration
63+
public @interface DataCouchbaseTest {
64+
65+
/**
66+
* Properties in form {@literal key=value} that should be added to the Spring
67+
* {@link Environment} before the test runs.
68+
* @return the properties to add
69+
*/
70+
String[] properties() default {};
71+
72+
/**
73+
* Determines if default filtering should be used with
74+
* {@link SpringBootApplication @SpringBootApplication}. By default no beans are
75+
* included.
76+
* @see #includeFilters()
77+
* @see #excludeFilters()
78+
* @return if default filters should be used
79+
*/
80+
boolean useDefaultFilters() default true;
81+
82+
/**
83+
* A set of include filters which can be used to add otherwise filtered beans to the
84+
* application context.
85+
* @return include filters to apply
86+
*/
87+
Filter[] includeFilters() default {};
88+
89+
/**
90+
* A set of exclude filters which can be used to filter beans that would otherwise be
91+
* added to the application context.
92+
* @return exclude filters to apply
93+
*/
94+
Filter[] excludeFilters() default {};
95+
96+
/**
97+
* Auto-configuration exclusions that should be applied for this test.
98+
* @return auto-configuration exclusions to apply
99+
*/
100+
@AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude")
101+
Class<?>[] excludeAutoConfiguration() default {};
102+
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2022 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.couchbase;
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 DataCouchbaseTest @DataCouchbaseTest}
26+
* support.
27+
*
28+
* @author Eddú Meléndez
29+
*/
30+
class DataCouchbaseTestContextBootstrapper extends SpringBootTestContextBootstrapper {
31+
32+
@Override
33+
protected String[] getProperties(Class<?> testClass) {
34+
return MergedAnnotations.from(testClass, SearchStrategy.INHERITED_ANNOTATIONS).get(DataCouchbaseTest.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-2022 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.couchbase;
18+
19+
import org.springframework.boot.context.TypeExcludeFilter;
20+
import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter;
21+
22+
/**
23+
* {@link TypeExcludeFilter} for {@link DataCouchbaseTest @DataCouchbaseaTest}.
24+
*
25+
* @author Eddú Meléndez
26+
*/
27+
class DataCouchbaseTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter<DataCouchbaseTest> {
28+
29+
protected DataCouchbaseTypeExcludeFilter(Class<?> testClass) {
30+
super(testClass);
31+
}
32+
33+
}

0 commit comments

Comments
 (0)