Add DataCouchbaseTest annotation

See gh-29912
pull/30016/head
Eddú Meléndez 3 years ago committed by Andy Wilkinson
parent 8015fb0608
commit 36e52758a7

@ -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-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-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-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-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-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 boot-features-testing-spring-boot-applications-testing-autoconfigured-data-jdbc-test=features.testing.spring-boot-applications.autoconfigured-spring-data-jdbc

@ -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 "<<data#data.nosql.couchbase>>", earlier in this chapter.)
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>>.
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]] [[features.testing.spring-boot-applications.autoconfigured-spring-data-elasticsearch]]
==== Auto-configured Data Elasticsearch Tests ==== Auto-configured Data Elasticsearch Tests
You can use `@DataElasticsearchTest` to test Elasticsearch applications. You can use `@DataElasticsearchTest` to test Elasticsearch applications.

@ -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;
// ...
}

@ -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 {
}

@ -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) {
// ...
}

@ -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

@ -48,6 +48,7 @@ dependencies {
optional("org.springframework.data:spring-data-cassandra") { optional("org.springframework.data:spring-data-cassandra") {
exclude group: "org.slf4j", module: "jcl-over-slf4j" 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-elasticsearch")
optional("org.springframework.data:spring-data-jdbc") optional("org.springframework.data:spring-data-jdbc")
optional("org.springframework.data:spring-data-jpa") optional("org.springframework.data:spring-data-jpa")
@ -109,6 +110,7 @@ dependencies {
testImplementation("org.springframework.hateoas:spring-hateoas") testImplementation("org.springframework.hateoas:spring-hateoas")
testImplementation("org.springframework.plugin:spring-plugin-core") testImplementation("org.springframework.plugin:spring-plugin-core")
testImplementation("org.testcontainers:cassandra") testImplementation("org.testcontainers:cassandra")
testImplementation("org.testcontainers:couchbase")
testImplementation("org.testcontainers:elasticsearch") testImplementation("org.testcontainers:elasticsearch")
testImplementation("org.testcontainers:junit-jupiter") testImplementation("org.testcontainers:junit-jupiter")
testImplementation("org.testcontainers:mongodb") testImplementation("org.testcontainers:mongodb")

@ -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 {
}

@ -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 <strong>only</strong> on
* Couchbase components.
* <p>
* Using this annotation will disable full auto-configuration and instead apply only
* configuration relevant to Couchbase tests.
* <p>
* 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 {};
}

@ -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);
}
}

@ -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<DataCouchbaseTest> {
protected DataCouchbaseTypeExcludeFilter(Class<?> testClass) {
super(testClass);
}
}

@ -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;

@ -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

@ -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();
}
}

@ -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");
}
}

@ -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();
}
}

@ -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();
}
}

@ -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 {
}

@ -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;
}
}

@ -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<ExampleDocument, String> {
}

@ -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<ExampleDocument, String> {
}

@ -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);
}
}
Loading…
Cancel
Save