Support ServiceConnection beans in slice tests

Previously, to use `@ServiceConnection` on a `@Bean` method in a sliced
test required ServiceConnectionAutoConfiguration to be imported using
`@ImportAutoConfiguration`. This commit removes the need for this
import by registering ServiceConnectionAutoConfiguration in specific
slice test annotations.

See gh-36037
This commit is contained in:
Eddú Meléndez 2023-06-22 20:28:19 -06:00 committed by Andy Wilkinson
parent 523d86d48e
commit 4dfb60c9a3
18 changed files with 507 additions and 0 deletions

View File

@ -0,0 +1,90 @@
/*
* Copyright 2012-2023 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.cassandra;
import java.util.UUID;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.CassandraContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.cassandra.core.CassandraTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test for {@link DataCassandraTest @DataCassandraTest}.
*
* @author Artsiom Yudovin
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Eddú Meléndez
*/
@DataCassandraTest(properties = { "spring.cassandra.schema-action=create-if-not-exists",
"spring.cassandra.connection.connect-timeout=60s", "spring.cassandra.connection.init-query-timeout=60s",
"spring.cassandra.request.timeout=60s" })
@Testcontainers(disabledWithoutDocker = true)
class DataCassandraTestWithServiceConnectionBeanIntegrationTests {
@Autowired
private CassandraTemplate cassandraTemplate;
@Autowired
private ExampleRepository exampleRepository;
@Test
void testRepository() {
ExampleEntity entity = new ExampleEntity();
entity.setDescription("Look, new @DataCassandraTest!");
String id = UUID.randomUUID().toString();
entity.setId(id);
ExampleEntity savedEntity = this.exampleRepository.save(entity);
ExampleEntity getEntity = this.cassandraTemplate.selectOneById(id, ExampleEntity.class);
assertThat(getEntity).isNotNull();
assertThat(getEntity.getId()).isNotNull();
assertThat(getEntity.getId()).isEqualTo(savedEntity.getId());
this.exampleRepository.deleteAll();
}
@TestConfiguration(proxyBeanMethods = false)
static class KeyspaceTestConfiguration {
@Bean
@ServiceConnection
CassandraContainer cassandra() {
return new CassandraContainer();
}
@Bean
CqlSession cqlSession(CqlSessionBuilder cqlSessionBuilder) {
try (CqlSession session = cqlSessionBuilder.build()) {
session.execute("CREATE KEYSPACE IF NOT EXISTS boot_test"
+ " WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };");
}
return cqlSessionBuilder.withKeyspace("boot_test").build();
}
}
}

View File

@ -0,0 +1,79 @@
/*
* Copyright 2012-2023 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.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Bean;
import org.springframework.data.couchbase.core.CouchbaseTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test for {@link DataCouchbaseTest @DataCouchbaseTest}.
*
* @author Eddú Meléndez
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
*/
@DataCouchbaseTest(
properties = { "spring.couchbase.env.timeouts.connect=2m", "spring.data.couchbase.bucket-name=cbbucket" })
@Testcontainers(disabledWithoutDocker = true)
class DataCouchbaseTestWithServiceConnectionBeanIntegrationTests {
private static final String BUCKET_NAME = "cbbucket";
@Autowired
private CouchbaseTemplate couchbaseTemplate;
@Autowired
private ExampleRepository exampleRepository;
@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();
}
@TestConfiguration(proxyBeanMethods = false)
static class ContainerConfig {
@Bean
@ServiceConnection
CouchbaseContainer couchbase() {
return new CouchbaseContainer(DockerImageNames.couchbase()).withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(10))
.withBucket(new BucketDefinition(BUCKET_NAME));
}
}
}

View File

@ -0,0 +1,81 @@
/*
* Copyright 2012-2023 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.elasticsearch;
import java.time.Duration;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Bean;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Sample test for {@link DataElasticsearchTest @DataElasticsearchTest}
*
* @author Eddú Meléndez
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
*/
@DataElasticsearchTest
@Testcontainers(disabledWithoutDocker = true)
class DataElasticsearchTestWithServiceConnectionBeanIntegrationTests {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired
private ExampleRepository exampleRepository;
@Test
void testRepository() {
ExampleDocument document = new ExampleDocument();
document.setText("Look, new @DataElasticsearchTest!");
String id = UUID.randomUUID().toString();
document.setId(id);
ExampleDocument savedDocument = this.exampleRepository.save(document);
ExampleDocument getDocument = this.elasticsearchTemplate.get(id, ExampleDocument.class);
assertThat(getDocument).isNotNull();
assertThat(getDocument.getId()).isNotNull();
assertThat(getDocument.getId()).isEqualTo(savedDocument.getId());
this.exampleRepository.deleteAll();
}
@TestConfiguration(proxyBeanMethods = false)
static class ContainerConfig {
@Bean
@ServiceConnection
ElasticsearchContainer elasticsearch() {
return new ElasticsearchContainer(DockerImageNames.elasticsearch())
.withEnv("ES_JAVA_OPTS", "-Xms32m -Xmx512m")
.withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(10));
}
}
}

View File

@ -0,0 +1,74 @@
/*
* Copyright 2012-2023 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.mongo;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.MongoTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Sample test for {@link DataMongoTest @DataMongoTest}
*
* @author Michael Simons
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Eddú Meléndez
*/
@DataMongoTest
@Testcontainers(disabledWithoutDocker = true)
class DataMongoTestWithServiceConnectionBeanIntegrationTests {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private ExampleRepository exampleRepository;
@Test
void testRepository() {
ExampleDocument exampleDocument = new ExampleDocument();
exampleDocument.setText("Look, new @DataMongoTest!");
exampleDocument = this.exampleRepository.save(exampleDocument);
assertThat(exampleDocument.getId()).isNotNull();
assertThat(this.mongoTemplate.collectionExists("exampleDocuments")).isTrue();
}
@TestConfiguration(proxyBeanMethods = false)
static class ContainerConfig {
@Bean
@ServiceConnection
MongoDBContainer mongoDB() {
return new MongoDBContainer(DockerImageNames.mongo()).withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(5));
}
}
}

View File

@ -0,0 +1,79 @@
/*
* Copyright 2012-2023 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.neo4j;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.Neo4jContainer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.DockerImageNames;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.neo4j.core.Neo4jTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test for {@link DataNeo4jTest @DataNeo4jTest}.
*
* @author Eddú Meléndez
* @author Stephane Nicoll
* @author Michael Simons
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
*/
@DataNeo4jTest
@Testcontainers(disabledWithoutDocker = true)
class DataNeo4jTestWithServiceConnectionBeanIntegrationTests {
@Autowired
private Neo4jTemplate neo4jTemplate;
@Autowired
private ExampleRepository exampleRepository;
@Autowired
private ApplicationContext applicationContext;
@Test
void testRepository() {
ExampleGraph exampleGraph = new ExampleGraph("Look, new @DataNeo4jTest!");
assertThat(exampleGraph.getId()).isNull();
ExampleGraph savedGraph = this.exampleRepository.save(exampleGraph);
assertThat(savedGraph.getId()).isNotNull();
assertThat(this.neo4jTemplate.count(ExampleGraph.class)).isOne();
}
@TestConfiguration
static class ContainerConfig {
@Bean
@ServiceConnection
Neo4jContainer<?> neo4j() {
return new Neo4jContainer<>(DockerImageNames.neo4j()).withStartupAttempts(5)
.withStartupTimeout(Duration.ofMinutes(10));
}
}
}

View File

@ -0,0 +1,80 @@
/*
* Copyright 2012-2023 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.redis;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.boot.testsupport.testcontainers.RedisContainer;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisOperations;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test for {@link DataRedisTest @DataRedisTest}.
*
* @author Jayaram Pradhan
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Eddú Meléndez
*/
@Testcontainers(disabledWithoutDocker = true)
@DataRedisTest
class DataRedisTestWithServiceConnectionBeanIntegrationTests {
private static final Charset CHARSET = StandardCharsets.UTF_8;
@Autowired
private RedisOperations<Object, Object> operations;
@Autowired
private ExampleRepository exampleRepository;
@Test
void testRepository() {
PersonHash personHash = new PersonHash();
personHash.setDescription("Look, new @DataRedisTest!");
assertThat(personHash.getId()).isNull();
PersonHash savedEntity = this.exampleRepository.save(personHash);
assertThat(savedEntity.getId()).isNotNull();
assertThat(this.operations
.execute((org.springframework.data.redis.connection.RedisConnection connection) -> connection.keyCommands()
.exists(("persons:" + savedEntity.getId()).getBytes(CHARSET))))
.isTrue();
this.exampleRepository.deleteAll();
}
@TestConfiguration(proxyBeanMethods = false)
static class ContainerConfig {
@Bean
@ServiceConnection(name = "redis")
RedisContainer redis() {
return new RedisContainer();
}
}
}

View File

@ -0,0 +1,2 @@
# AutoConfigureDataCassandra auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@ -0,0 +1,2 @@
# AutoConfigureDataCouchbase auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@ -0,0 +1,2 @@
# AutoConfigureDataElasticsearch auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@ -0,0 +1,2 @@
# AutoConfigureDataJdbc auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@ -0,0 +1,2 @@
# AutoConfigureDataMongo auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@ -0,0 +1,2 @@
# AutoConfigureDataNeo4j auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@ -0,0 +1,2 @@
# AutoConfigureDataR2dbc auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@ -0,0 +1,2 @@
# AutoConfigureDataRedis auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@ -0,0 +1,2 @@
# AutoConfigureJdbc auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@ -0,0 +1,2 @@
# AutoConfigureTestDatabase auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@ -0,0 +1,2 @@
# AutoConfigureJooq auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration

View File

@ -0,0 +1,2 @@
# AutoConfigureDataJpa auto-configuration imports
org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration