From 36e52758a74e0cc7139833b429ff81645ddbd693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Sat, 19 Feb 2022 20:20:37 -0600 Subject: [PATCH 1/2] Add DataCouchbaseTest annotation See gh-29912 --- .../docs/asciidoc/anchor-rewrite.properties | 1 + .../src/docs/asciidoc/features/testing.adoc | 16 +++ .../MyDataCouchbaseTests.java | 31 ++++++ .../SomeRepository.java | 21 ++++ .../MyDataCouchbaseTests.kt | 27 +++++ .../SomeRepository.kt | 19 ++++ .../build.gradle | 2 + .../couchbase/AutoConfigureDataCouchbase.java | 44 ++++++++ .../data/couchbase/DataCouchbaseTest.java | 103 ++++++++++++++++++ .../DataCouchbaseTestContextBootstrapper.java | 38 +++++++ .../DataCouchbaseTypeExcludeFilter.java | 33 ++++++ .../data/couchbase/package-info.java | 20 ++++ ...uchbase.AutoConfigureDataCouchbase.imports | 7 ++ .../DataCouchbaseTestIntegrationTests.java | 87 +++++++++++++++ ...uchbaseTestPropertiesIntegrationTests.java | 43 ++++++++ ...CouchbaseTestReactiveIntegrationTests.java | 80 ++++++++++++++ ...TestWithIncludeFilterIntegrationTests.java | 75 +++++++++++++ .../ExampleCouchbaseApplication.java | 30 +++++ .../data/couchbase/ExampleDocument.java | 54 +++++++++ .../couchbase/ExampleReactiveRepository.java | 29 +++++ .../data/couchbase/ExampleRepository.java | 28 +++++ .../data/couchbase/ExampleService.java | 40 +++++++ 22 files changed, 828 insertions(+) create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/MyDataCouchbaseTests.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/SomeRepository.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/MyDataCouchbaseTests.kt create mode 100644 spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/SomeRepository.kt create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/AutoConfigureDataCouchbase.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTest.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestContextBootstrapper.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTypeExcludeFilter.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/package-info.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.data.couchbase.AutoConfigureDataCouchbase.imports create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestIntegrationTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestPropertiesIntegrationTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestReactiveIntegrationTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestWithIncludeFilterIntegrationTests.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleCouchbaseApplication.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleDocument.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleReactiveRepository.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleRepository.java create mode 100644 spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleService.java diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties index 7f7b5c612d1..51247f24114 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties @@ -339,6 +339,7 @@ boot-features-testing-spring-boot-applications-testing-autoconfigured-json-tests boot-features-testing-spring-boot-applications-testing-autoconfigured-mvc-tests=features.testing.spring-boot-applications.spring-mvc-tests boot-features-testing-spring-boot-applications-testing-autoconfigured-webflux-tests=features.testing.spring-boot-applications.spring-webflux-tests boot-features-testing-spring-boot-applications-testing-autoconfigured-cassandra-test=features.testing.spring-boot-applications.autoconfigured-spring-data-cassandra +boot-features-testing-spring-boot-applications-testing-autoconfigured-couchbase-test=features.testing.spring-boot-applications.autoconfigured-spring-data-couchbase boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test=features.testing.spring-boot-applications.autoconfigured-spring-data-jpa boot-features-testing-spring-boot-applications-testing-autoconfigured-jdbc-test=features.testing.spring-boot-applications.autoconfigured-jdbc boot-features-testing-spring-boot-applications-testing-autoconfigured-data-jdbc-test=features.testing.spring-boot-applications.autoconfigured-spring-data-jdbc diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc index 4c8f4f34167..2fe2842b138 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc @@ -472,6 +472,22 @@ include::code:MyDataCassandraTests[] +[[features.testing.spring-boot-applications.autoconfigured-spring-data-couchbase]] +==== Auto-configured Data Couchbase Tests +You can use `@DataCouchbaseTest` to test Couchbase applications. +By default, it configures a `CouchbaseTemplate`, scans for `@Document` classes, and configures Spring Data Couchbase repositories. +Regular `@Component` and `@ConfigurationProperties` beans are not scanned when the `@DataCouchbaseTest` annotation is used. +`@EnableConfigurationProperties` can be used to include `@ConfigurationProperties` beans. +(For more about using Couchbase with Spring Boot, see "<>", earlier in this chapter.) + +TIP: A list of the auto-configuration settings that are enabled by `@DataCouchbaseTest` can be <>. + +The following example shows a typical setup for using Couchbase tests in Spring Boot: + +include::code:MyDataCouchbaseTests[] + + + [[features.testing.spring-boot-applications.autoconfigured-spring-data-elasticsearch]] ==== Auto-configured Data Elasticsearch Tests You can use `@DataElasticsearchTest` to test Elasticsearch applications. diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/MyDataCouchbaseTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/MyDataCouchbaseTests.java new file mode 100644 index 00000000000..87fa8af5f4a --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/MyDataCouchbaseTests.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringdatacouchbase; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.data.couchbase.DataCouchbaseTest; + +@DataCouchbaseTest +class MyDataCouchbaseTests { + + @Autowired + @SuppressWarnings("unused") + private SomeRepository repository; + + // ... + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/SomeRepository.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/SomeRepository.java new file mode 100644 index 00000000000..f5f5bfb281c --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/SomeRepository.java @@ -0,0 +1,21 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringdatacouchbase; + +interface SomeRepository { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/MyDataCouchbaseTests.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/MyDataCouchbaseTests.kt new file mode 100644 index 00000000000..d2268b776c6 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/MyDataCouchbaseTests.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringdatacouchbase + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.data.couchbase.DataCouchbaseTest + +@DataCouchbaseTest +class MyDataCouchbaseTests(@Autowired val repository: SomeRepository) { + + // ... + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/SomeRepository.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/SomeRepository.kt new file mode 100644 index 00000000000..a9f8f797e7b --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredspringdatacouchbase/SomeRepository.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringdatacouchbase + +interface SomeRepository diff --git a/spring-boot-project/spring-boot-test-autoconfigure/build.gradle b/spring-boot-project/spring-boot-test-autoconfigure/build.gradle index ae26e979dfb..000ab1ebfc2 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/build.gradle +++ b/spring-boot-project/spring-boot-test-autoconfigure/build.gradle @@ -48,6 +48,7 @@ dependencies { optional("org.springframework.data:spring-data-cassandra") { exclude group: "org.slf4j", module: "jcl-over-slf4j" } + optional("org.springframework.data:spring-data-couchbase") optional("org.springframework.data:spring-data-elasticsearch") optional("org.springframework.data:spring-data-jdbc") optional("org.springframework.data:spring-data-jpa") @@ -109,6 +110,7 @@ dependencies { testImplementation("org.springframework.hateoas:spring-hateoas") testImplementation("org.springframework.plugin:spring-plugin-core") testImplementation("org.testcontainers:cassandra") + testImplementation("org.testcontainers:couchbase") testImplementation("org.testcontainers:elasticsearch") testImplementation("org.testcontainers:junit-jupiter") testImplementation("org.testcontainers:mongodb") diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/AutoConfigureDataCouchbase.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/AutoConfigureDataCouchbase.java new file mode 100644 index 00000000000..7515d1b169b --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/AutoConfigureDataCouchbase.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; + +/** + * {@link ImportAutoConfiguration Auto-configuration imports} for typical Data Couchbase + * tests. Most tests should consider using {@link DataCouchbaseTest @DataCouchbaseTest} + * rather than using this annotation directly. + * + * @author Eddú Meléndez + * @since 2.7.0 + * @see DataCouchbaseTest + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ImportAutoConfiguration +public @interface AutoConfigureDataCouchbase { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTest.java new file mode 100644 index 00000000000..f3aa83469aa --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache; +import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.core.annotation.AliasFor; +import org.springframework.core.env.Environment; +import org.springframework.test.context.BootstrapWith; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * Annotation that can be used for a Couchbase test that focuses only on + * Couchbase components. + *

+ * Using this annotation will disable full auto-configuration and instead apply only + * configuration relevant to Couchbase tests. + *

+ * When using JUnit 4, this annotation should be used in combination with + * {@code @RunWith(SpringRunner.class)}. + * + * @author Eddú Meléndez + * @since 2.7.0 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@BootstrapWith(DataCouchbaseTestContextBootstrapper.class) +@ExtendWith(SpringExtension.class) +@OverrideAutoConfiguration(enabled = false) +@TypeExcludeFilters(DataCouchbaseTypeExcludeFilter.class) +@AutoConfigureCache +@AutoConfigureDataCouchbase +@ImportAutoConfiguration +public @interface DataCouchbaseTest { + + /** + * Properties in form {@literal key=value} that should be added to the Spring + * {@link Environment} before the test runs. + * @return the properties to add + */ + String[] properties() default {}; + + /** + * Determines if default filtering should be used with + * {@link SpringBootApplication @SpringBootApplication}. By default no beans are + * included. + * @see #includeFilters() + * @see #excludeFilters() + * @return if default filters should be used + */ + boolean useDefaultFilters() default true; + + /** + * A set of include filters which can be used to add otherwise filtered beans to the + * application context. + * @return include filters to apply + */ + Filter[] includeFilters() default {}; + + /** + * A set of exclude filters which can be used to filter beans that would otherwise be + * added to the application context. + * @return exclude filters to apply + */ + Filter[] excludeFilters() default {}; + + /** + * Auto-configuration exclusions that should be applied for this test. + * @return auto-configuration exclusions to apply + */ + @AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude") + Class[] excludeAutoConfiguration() default {}; + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestContextBootstrapper.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestContextBootstrapper.java new file mode 100644 index 00000000000..f25b62de589 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestContextBootstrapper.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; +import org.springframework.core.annotation.MergedAnnotations; +import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; +import org.springframework.test.context.TestContextBootstrapper; + +/** + * {@link TestContextBootstrapper} for {@link DataCouchbaseTest @DataCouchbaseTest} + * support. + * + * @author Eddú Meléndez + */ +class DataCouchbaseTestContextBootstrapper extends SpringBootTestContextBootstrapper { + + @Override + protected String[] getProperties(Class testClass) { + return MergedAnnotations.from(testClass, SearchStrategy.INHERITED_ANNOTATIONS).get(DataCouchbaseTest.class) + .getValue("properties", String[].class).orElse(null); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTypeExcludeFilter.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTypeExcludeFilter.java new file mode 100644 index 00000000000..fe287ba08ac --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTypeExcludeFilter.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import org.springframework.boot.context.TypeExcludeFilter; +import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter; + +/** + * {@link TypeExcludeFilter} for {@link DataCouchbaseTest @DataCouchbaseaTest}. + * + * @author Eddú Meléndez + */ +class DataCouchbaseTypeExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter { + + protected DataCouchbaseTypeExcludeFilter(Class testClass) { + super(testClass); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/package-info.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/package-info.java new file mode 100644 index 00000000000..068cc64f405 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Auto-configuration for Data Couchbase tests. + */ +package org.springframework.boot.test.autoconfigure.data.couchbase; diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.data.couchbase.AutoConfigureDataCouchbase.imports b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.data.couchbase.AutoConfigureDataCouchbase.imports new file mode 100644 index 00000000000..aaae4207e95 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.test.autoconfigure.data.couchbase.AutoConfigureDataCouchbase.imports @@ -0,0 +1,7 @@ +# AutoConfigureDataCouchbase auto-configuration imports + +org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration +org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration +org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveDataAutoConfiguration +org.springframework.boot.autoconfigure.data.couchbase.CouchbaseReactiveRepositoriesAutoConfiguration +org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestIntegrationTests.java new file mode 100644 index 00000000000..d8c58eb8c35 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestIntegrationTests.java @@ -0,0 +1,87 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import java.time.Duration; + +import org.junit.jupiter.api.Test; +import org.testcontainers.couchbase.BucketDefinition; +import org.testcontainers.couchbase.CouchbaseContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; +import org.springframework.context.ApplicationContext; +import org.springframework.data.couchbase.core.CouchbaseTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; + +/** + * Integration test for {@link DataCouchbaseTest @DataCouchbaseTest}. + * + * @author Eddú Meléndez + */ +@DataCouchbaseTest +@Testcontainers(disabledWithoutDocker = true) +class DataCouchbaseTestIntegrationTests { + + private static final String BUCKET_NAME = "cbbucket"; + + @Container + static final CouchbaseContainer couchbase = new CouchbaseContainer(DockerImageNames.couchbase()) + .withStartupAttempts(5).withStartupTimeout(Duration.ofMinutes(10)) + .withBucket(new BucketDefinition(BUCKET_NAME)); + + @DynamicPropertySource + static void couchbaseProperties(DynamicPropertyRegistry registry) { + registry.add("spring.couchbase.connection-string", couchbase::getConnectionString); + registry.add("spring.couchbase.username", couchbase::getUsername); + registry.add("spring.couchbase.password", couchbase::getPassword); + registry.add("spring.data.couchbase.bucket-name", () -> BUCKET_NAME); + } + + @Autowired + private CouchbaseTemplate couchbaseTemplate; + + @Autowired + private ExampleRepository exampleRepository; + + @Autowired + private ApplicationContext applicationContext; + + @Test + void didNotInjectExampleService() { + assertThatExceptionOfType(NoSuchBeanDefinitionException.class) + .isThrownBy(() -> this.applicationContext.getBean(ExampleService.class)); + } + + @Test + void testRepository() { + ExampleDocument document = new ExampleDocument(); + document.setText("Look, new @DataCouchbaseTest!"); + document = this.exampleRepository.save(document); + assertThat(document.getId()).isNotNull(); + assertThat(this.couchbaseTemplate.getBucketName()).isEqualTo(BUCKET_NAME); + this.exampleRepository.deleteAll(); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestPropertiesIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestPropertiesIntegrationTests.java new file mode 100644 index 00000000000..c489be648b1 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestPropertiesIntegrationTests.java @@ -0,0 +1,43 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for the {@link DataCouchbaseTest#properties properties} attribute of + * {@link DataCouchbaseTest @DataCouchbaseTest}. + * + * @author Eddú Meléndez + */ +@DataCouchbaseTest(properties = "spring.profiles.active=test") +class DataCouchbaseTestPropertiesIntegrationTests { + + @Autowired + private Environment environment; + + @Test + void environmentWithNewProfile() { + assertThat(this.environment.getActiveProfiles()).containsExactly("test"); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestReactiveIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestReactiveIntegrationTests.java new file mode 100644 index 00000000000..b300f336038 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestReactiveIntegrationTests.java @@ -0,0 +1,80 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import java.time.Duration; + +import org.junit.jupiter.api.Test; +import org.testcontainers.couchbase.BucketDefinition; +import org.testcontainers.couchbase.CouchbaseContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; +import org.springframework.context.ApplicationContext; +import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Sample tests for {@link DataCouchbaseTest @DataCouchbaseTest} using reactive + * repositories. + * + * @author Eddú Meléndez + */ +@DataCouchbaseTest +@Testcontainers(disabledWithoutDocker = true) +class DataCouchbaseTestReactiveIntegrationTests { + + private static final String BUCKET_NAME = "cbbucket"; + + @Container + static final CouchbaseContainer couchbase = new CouchbaseContainer(DockerImageNames.couchbase()) + .withStartupAttempts(5).withStartupTimeout(Duration.ofMinutes(10)) + .withBucket(new BucketDefinition(BUCKET_NAME)); + + @DynamicPropertySource + static void couchbaseProperties(DynamicPropertyRegistry registry) { + registry.add("spring.couchbase.connection-string", couchbase::getConnectionString); + registry.add("spring.couchbase.username", couchbase::getUsername); + registry.add("spring.couchbase.password", couchbase::getPassword); + registry.add("spring.data.couchbase.bucket-name", () -> BUCKET_NAME); + } + + @Autowired + private ReactiveCouchbaseTemplate couchbaseTemplate; + + @Autowired + private ExampleReactiveRepository exampleReactiveRepository; + + @Autowired + private ApplicationContext applicationContext; + + @Test + void testRepository() { + ExampleDocument document = new ExampleDocument(); + document.setText("Look, new @DataCouchbaseTest!"); + document = this.exampleReactiveRepository.save(document).block(Duration.ofSeconds(30)); + assertThat(document.getId()).isNotNull(); + assertThat(this.couchbaseTemplate.getBucketName()).isEqualTo(BUCKET_NAME); + this.exampleReactiveRepository.deleteAll(); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestWithIncludeFilterIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestWithIncludeFilterIntegrationTests.java new file mode 100644 index 00000000000..ba8d393805c --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestWithIncludeFilterIntegrationTests.java @@ -0,0 +1,75 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import java.time.Duration; + +import org.junit.jupiter.api.Test; +import org.testcontainers.couchbase.BucketDefinition; +import org.testcontainers.couchbase.CouchbaseContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.testsupport.testcontainers.DockerImageNames; +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.stereotype.Service; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Integration test with custom include filter for + * {@link DataCouchbaseTest @DataCouchbaseTest}. + * + * @author Eddú Meléndez + */ +@DataCouchbaseTest(includeFilters = @Filter(Service.class)) +@Testcontainers(disabledWithoutDocker = true) +class DataCouchbaseTestWithIncludeFilterIntegrationTests { + + private static final String BUCKET_NAME = "cbbucket"; + + @Container + static final CouchbaseContainer couchbase = new CouchbaseContainer(DockerImageNames.couchbase()) + .withStartupAttempts(5).withStartupTimeout(Duration.ofMinutes(10)) + .withBucket(new BucketDefinition(BUCKET_NAME)); + + @DynamicPropertySource + static void couchbaseProperties(DynamicPropertyRegistry registry) { + registry.add("spring.couchbase.connection-string", couchbase::getConnectionString); + registry.add("spring.couchbase.username", couchbase::getUsername); + registry.add("spring.couchbase.password", couchbase::getPassword); + registry.add("spring.data.couchbase.bucket-name", () -> BUCKET_NAME); + } + + @Autowired + private ExampleRepository exampleRepository; + + @Autowired + private ExampleService service; + + @Test + void testService() { + ExampleDocument document = new ExampleDocument(); + document.setText("Look, new @DataCouchbaseTest!"); + document = this.exampleRepository.save(document); + assertThat(this.service.findById(document.getId())).isNotNull(); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleCouchbaseApplication.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleCouchbaseApplication.java new file mode 100644 index 00000000000..a339f8d3b12 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleCouchbaseApplication.java @@ -0,0 +1,30 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Example {@link SpringBootApplication @SpringBootApplication} used with + * {@link DataCouchbaseTest @DataCouchbaseTest} tests. + * + * @author Eddú Meléndez + */ +@SpringBootApplication +public class ExampleCouchbaseApplication { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleDocument.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleDocument.java new file mode 100644 index 00000000000..e008214a5cd --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleDocument.java @@ -0,0 +1,54 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import org.springframework.data.annotation.Id; +import org.springframework.data.couchbase.core.mapping.Document; +import org.springframework.data.couchbase.core.mapping.id.GeneratedValue; +import org.springframework.data.couchbase.core.mapping.id.GenerationStrategy; + +/** + * Example document used with {@link DataCouchbaseTest @DataCouchbaseTest} tests. + * + * @author Eddú Meléndez + */ +@Document +public class ExampleDocument { + + @Id + @GeneratedValue(strategy = GenerationStrategy.UNIQUE) + private String id; + + private String text; + + public String getId() { + return this.id; + } + + public void setId(String id) { + this.id = id; + } + + public String getText() { + return this.text; + } + + public void setText(String text) { + this.text = text; + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleReactiveRepository.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleReactiveRepository.java new file mode 100644 index 00000000000..dfd769babf2 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleReactiveRepository.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import org.springframework.data.couchbase.repository.ReactiveCouchbaseRepository; + +/** + * Example reactive repository used with {@link DataCouchbaseTest @DataCouchbaseTest} + * tests. + * + * @author Eddú Meléndez + */ +interface ExampleReactiveRepository extends ReactiveCouchbaseRepository { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleRepository.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleRepository.java new file mode 100644 index 00000000000..0567e1b40fd --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleRepository.java @@ -0,0 +1,28 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import org.springframework.data.couchbase.repository.CouchbaseRepository; + +/** + * Example repository used with {@link DataCouchbaseTest @DataCouchbaseTest} tests. + * + * @author Eddú Meléndez + */ +interface ExampleRepository extends CouchbaseRepository { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleService.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleService.java new file mode 100644 index 00000000000..82353a57c1d --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/ExampleService.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.data.couchbase; + +import org.springframework.data.couchbase.core.CouchbaseTemplate; +import org.springframework.stereotype.Service; + +/** + * Example service used with {@link DataCouchbaseTest @DataCouchbaseTest} tests. + * + * @author Eddú Meléndez + */ +@Service +public class ExampleService { + + private final CouchbaseTemplate couchbaseTemplate; + + public ExampleService(CouchbaseTemplate couchbaseTemplate) { + this.couchbaseTemplate = couchbaseTemplate; + } + + public ExampleDocument findById(String id) { + return this.couchbaseTemplate.findById(ExampleDocument.class).one(id); + } + +} From 664ecbdcc18d661db298bfe7f6255dfef33a5d07 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 24 Feb 2022 11:51:46 +0000 Subject: [PATCH 2/2] Polish "Add DataCouchbaseTest annotation" See gh-29912 --- .../src/docs/asciidoc/anchor-rewrite.properties | 1 - .../src/docs/asciidoc/features/testing.adoc | 2 +- .../autoconfigure/data/couchbase/DataCouchbaseTest.java | 6 +++--- .../DataCouchbaseTestReactiveIntegrationTests.java | 4 ---- 4 files changed, 4 insertions(+), 9 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties index 51247f24114..7f7b5c612d1 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/anchor-rewrite.properties @@ -339,7 +339,6 @@ boot-features-testing-spring-boot-applications-testing-autoconfigured-json-tests boot-features-testing-spring-boot-applications-testing-autoconfigured-mvc-tests=features.testing.spring-boot-applications.spring-mvc-tests boot-features-testing-spring-boot-applications-testing-autoconfigured-webflux-tests=features.testing.spring-boot-applications.spring-webflux-tests boot-features-testing-spring-boot-applications-testing-autoconfigured-cassandra-test=features.testing.spring-boot-applications.autoconfigured-spring-data-cassandra -boot-features-testing-spring-boot-applications-testing-autoconfigured-couchbase-test=features.testing.spring-boot-applications.autoconfigured-spring-data-couchbase boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test=features.testing.spring-boot-applications.autoconfigured-spring-data-jpa boot-features-testing-spring-boot-applications-testing-autoconfigured-jdbc-test=features.testing.spring-boot-applications.autoconfigured-jdbc boot-features-testing-spring-boot-applications-testing-autoconfigured-data-jdbc-test=features.testing.spring-boot-applications.autoconfigured-spring-data-jdbc diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc index 2fe2842b138..772ffa47403 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc @@ -475,7 +475,7 @@ include::code:MyDataCassandraTests[] [[features.testing.spring-boot-applications.autoconfigured-spring-data-couchbase]] ==== Auto-configured Data Couchbase Tests You can use `@DataCouchbaseTest` to test Couchbase applications. -By default, it configures a `CouchbaseTemplate`, scans for `@Document` classes, and configures Spring Data Couchbase repositories. +By default, it configures a `CouchbaseTemplate` or `ReactiveCouchbaseTemplate`, scans for `@Document` classes, and configures Spring Data Couchbase repositories. Regular `@Component` and `@ConfigurationProperties` beans are not scanned when the `@DataCouchbaseTest` annotation is used. `@EnableConfigurationProperties` can be used to include `@ConfigurationProperties` beans. (For more about using Couchbase with Spring Boot, see "<>", earlier in this chapter.) diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTest.java index f3aa83469aa..0e52d6f6617 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTest.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTest.java @@ -37,11 +37,11 @@ import org.springframework.test.context.BootstrapWith; import org.springframework.test.context.junit.jupiter.SpringExtension; /** - * Annotation that can be used for a Couchbase test that focuses only on - * Couchbase components. + * Annotation that can be used for a Data Couchbase test that focuses + * only on Data Couchbase components. *

* Using this annotation will disable full auto-configuration and instead apply only - * configuration relevant to Couchbase tests. + * configuration relevant to Data Couchbase tests. *

* When using JUnit 4, this annotation should be used in combination with * {@code @RunWith(SpringRunner.class)}. diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestReactiveIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestReactiveIntegrationTests.java index b300f336038..8ce3bc0e9bb 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestReactiveIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/couchbase/DataCouchbaseTestReactiveIntegrationTests.java @@ -26,7 +26,6 @@ import org.testcontainers.junit.jupiter.Testcontainers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.testsupport.testcontainers.DockerImageNames; -import org.springframework.context.ApplicationContext; import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; @@ -64,9 +63,6 @@ class DataCouchbaseTestReactiveIntegrationTests { @Autowired private ExampleReactiveRepository exampleReactiveRepository; - @Autowired - private ApplicationContext applicationContext; - @Test void testRepository() { ExampleDocument document = new ExampleDocument();