forked from spring-projects/spring-boot
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support ServiceConnection beans in slice tests
Currently, in order to use `@ServiceConnection` in beans along with slice test annotations `@ImportAutoConfiguration(ServiceConnectionAutoConfiguration.class)` should be added to make it work. This commit register `ServiceConnectionAutoConfiguration` in specific slice test annotations. See spring-projectsgh-35252
- Loading branch information
1 parent
e33cec5
commit 96444ee
Showing
20 changed files
with
546 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...st/autoconfigure/service/connection/TestcontainersServiceConnectionAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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.service.connection; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.AutoConfigureOrder; | ||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnectionAutoConfiguration; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.Ordered; | ||
|
||
/** | ||
* Auto-configuration for ServiceConnection. | ||
* | ||
* @author Eddú Meléndez | ||
* @since 3.1.2 | ||
*/ | ||
@AutoConfiguration | ||
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) | ||
@ConditionalOnClass(ServiceConnectionAutoConfiguration.class) | ||
public class TestcontainersServiceConnectionAutoConfiguration { | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
@ImportAutoConfiguration(ServiceConnectionAutoConfiguration.class) | ||
static class ImportAutoConfigurationTest { | ||
|
||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...springframework.boot.test.autoconfigure.data.cassandra.AutoConfigureDataCassandra.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...springframework.boot.test.autoconfigure.data.couchbase.AutoConfigureDataCouchbase.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...amework.boot.test.autoconfigure.data.elasticsearch.AutoConfigureDataElasticsearch.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...pring/org.springframework.boot.test.autoconfigure.data.jdbc.AutoConfigureDataJdbc.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ing/org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ing/org.springframework.boot.test.autoconfigure.data.neo4j.AutoConfigureDataNeo4j.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ing/org.springframework.boot.test.autoconfigure.data.r2dbc.AutoConfigureDataR2dbc.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...ing/org.springframework.boot.test.autoconfigure.data.redis.AutoConfigureDataRedis.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# AutoConfigureDataRedis auto-configuration imports | ||
org.springframework.boot.test.autoconfigure.service.connection.TestcontainersServiceConnectionAutoConfiguration | ||
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration | ||
org.springframework.boot.autoconfigure.data.redis.RedisReactiveAutoConfiguration | ||
org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration |
1 change: 1 addition & 0 deletions
1
...ETA-INF/spring/org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureJdbc.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...spring/org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# AutoConfigureTestDatabase auto-configuration imports | ||
org.springframework.boot.test.autoconfigure.service.connection.TestcontainersServiceConnectionAutoConfiguration | ||
org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration | ||
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration |
1 change: 1 addition & 0 deletions
1
...ETA-INF/spring/org.springframework.boot.test.autoconfigure.jooq.AutoConfigureJooq.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...F/spring/org.springframework.boot.test.autoconfigure.orm.jpa.AutoConfigureDataJpa.imports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...oconfigure/data/cassandra/DataCassandraTestWithServiceConnectionBeanIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
...oconfigure/data/couchbase/DataCouchbaseTestWithServiceConnectionBeanIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* 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.NoSuchBeanDefinitionException; | ||
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.couchbase.core.CouchbaseTemplate; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
|
||
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 | ||
* @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)); | ||
} | ||
|
||
} | ||
|
||
} |
83 changes: 83 additions & 0 deletions
83
...re/data/elasticsearch/DataElasticsearchTestWithServiceConnectionBeanIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* 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)); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.