Merge branch '2.7.x'
This commit is contained in:
		
						commit
						4093fff225
					
				|  | @ -419,6 +419,22 @@ include::code:MyDataCassandraTests[] | |||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| [[features.testing.spring-boot-applications.autoconfigured-spring-data-elasticsearch]] | ||||
| ==== Auto-configured Data Elasticsearch Tests | ||||
| You can use `@DataElasticsearchTest` to test Elasticsearch applications. | ||||
| By default, it configures a `ElasticsearchRestTemplate`, scans for `@Document` classes, and configures Spring Data Elasticsearch repositories. | ||||
| Regular `@Component` and `@ConfigurationProperties` beans are not scanned when the `@DataElasticsearchTest` annotation is used. | ||||
| `@EnableConfigurationProperties` can be used to include `@ConfigurationProperties` beans. | ||||
| (For more about using Elasticsearch with Spring Boot, see "<<data#data.nosql.elasticsearch>>", earlier in this chapter.) | ||||
| 
 | ||||
| TIP: A list of the auto-configuration settings that are enabled by `@DataElasticsearchTest` can be <<test-auto-configuration#appendix.test-auto-configuration,found in the appendix>>. | ||||
| 
 | ||||
| The following example shows a typical setup for using Elasticsearch tests in Spring Boot: | ||||
| 
 | ||||
| include::code:MyDataElasticsearchTests[] | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| [[features.testing.spring-boot-applications.autoconfigured-spring-data-jpa]] | ||||
| ==== Auto-configured Data JPA Tests | ||||
| You can use the `@DataJpaTest` annotation to test JPA applications. | ||||
|  |  | |||
|  | @ -0,0 +1,31 @@ | |||
| /* | ||||
|  * 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.autoconfiguredspringdataelasticsearch; | ||||
| 
 | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.boot.test.autoconfigure.data.elasticsearch.DataElasticsearchTest; | ||||
| 
 | ||||
| @DataElasticsearchTest | ||||
| public class MyDataElasticsearchTests { | ||||
| 
 | ||||
| 	@Autowired | ||||
| 	@SuppressWarnings("unused") | ||||
| 	private SomeRepository repository; | ||||
| 
 | ||||
| 	// ... | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,21 @@ | |||
| /* | ||||
|  * 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.autoconfiguredspringdataelasticsearch; | ||||
| 
 | ||||
| public 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.autoconfiguredspringdataelasticsearch | ||||
| 
 | ||||
| import org.springframework.beans.factory.annotation.Autowired | ||||
| import org.springframework.boot.test.autoconfigure.data.elasticsearch.DataElasticsearchTest | ||||
| 
 | ||||
| @DataElasticsearchTest | ||||
| class MyDataElasticsearchTests(@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.autoconfiguredspringdataelasticsearch | ||||
| 
 | ||||
| interface SomeRepository | ||||
|  | @ -42,6 +42,7 @@ dependencies { | |||
| 	optional("org.springframework.data:spring-data-cassandra") { | ||||
| 		exclude group: "org.slf4j", module: "jcl-over-slf4j" | ||||
| 	} | ||||
| 	optional("org.springframework.data:spring-data-elasticsearch") | ||||
| 	optional("org.springframework.data:spring-data-jdbc") | ||||
| 	optional("org.springframework.data:spring-data-jpa") | ||||
| 	optional("org.springframework.data:spring-data-ldap") | ||||
|  | @ -75,6 +76,7 @@ dependencies { | |||
| 	testImplementation("com.unboundid:unboundid-ldapsdk") | ||||
| 	testImplementation("io.lettuce:lettuce-core") | ||||
| 	testImplementation("io.micrometer:micrometer-registry-prometheus") | ||||
| 	testImplementation("io.projectreactor.netty:reactor-netty-http") | ||||
| 	testImplementation("io.projectreactor:reactor-core") | ||||
| 	testImplementation("io.projectreactor:reactor-test") | ||||
| 	testImplementation("io.r2dbc:r2dbc-h2") | ||||
|  | @ -100,6 +102,7 @@ dependencies { | |||
| 	testImplementation("org.springframework.hateoas:spring-hateoas") | ||||
| 	testImplementation("org.springframework.plugin:spring-plugin-core") | ||||
| 	testImplementation("org.testcontainers:cassandra") | ||||
| 	testImplementation("org.testcontainers:elasticsearch") | ||||
| 	testImplementation("org.testcontainers:junit-jupiter") | ||||
| 	testImplementation("org.testcontainers:mongodb") | ||||
| 	testImplementation("org.testcontainers:neo4j") | ||||
|  |  | |||
|  | @ -0,0 +1,45 @@ | |||
| /* | ||||
|  * 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.elasticsearch; | ||||
| 
 | ||||
| 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 | ||||
|  * Elasticsearch tests. Most tests should consider using | ||||
|  * {@link DataElasticsearchTest @DataElasticsearchTest} rather than using this annotation | ||||
|  * directly. | ||||
|  * | ||||
|  * @author Eddú Meléndez | ||||
|  * @since 2.7.0 | ||||
|  * @see DataElasticsearchTest | ||||
|  */ | ||||
| @Target(ElementType.TYPE) | ||||
| @Retention(RetentionPolicy.RUNTIME) | ||||
| @Documented | ||||
| @Inherited | ||||
| @ImportAutoConfiguration | ||||
| public @interface AutoConfigureDataElasticsearch { | ||||
| 
 | ||||
| } | ||||
|  | @ -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.elasticsearch; | ||||
| 
 | ||||
| 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 Data Elasticsearch test that focuses | ||||
|  * <strong>only</strong> on Data Elasticsearch components. | ||||
|  * <p> | ||||
|  * Using this annotation will disable full auto-configuration and instead apply only | ||||
|  * configuration relevant to Data Elasticsearch 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(DataElasticsearchTestContextBootstrapper.class) | ||||
| @ExtendWith(SpringExtension.class) | ||||
| @OverrideAutoConfiguration(enabled = false) | ||||
| @TypeExcludeFilters(DataElasticsearchTypeExcludeFilter.class) | ||||
| @AutoConfigureCache | ||||
| @AutoConfigureDataElasticsearch | ||||
| @ImportAutoConfiguration | ||||
| public @interface DataElasticsearchTest { | ||||
| 
 | ||||
| 	/** | ||||
| 	 * 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.elasticsearch; | ||||
| 
 | ||||
| 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 DataElasticsearchTest @DataElasticsearchTest} support. | ||||
|  * | ||||
|  * @author Eddú Meléndez | ||||
|  */ | ||||
| class DataElasticsearchTestContextBootstrapper extends SpringBootTestContextBootstrapper { | ||||
| 
 | ||||
| 	@Override | ||||
| 	protected String[] getProperties(Class<?> testClass) { | ||||
| 		return MergedAnnotations.from(testClass, SearchStrategy.INHERITED_ANNOTATIONS).get(DataElasticsearchTest.class) | ||||
| 				.getValue("properties", String[].class).orElse(null); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,34 @@ | |||
| /* | ||||
|  * 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.elasticsearch; | ||||
| 
 | ||||
| import org.springframework.boot.context.TypeExcludeFilter; | ||||
| import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter; | ||||
| 
 | ||||
| /** | ||||
|  * {@link TypeExcludeFilter} for {@link DataElasticsearchTest @DataElasticsearchTest}. | ||||
|  * | ||||
|  * @author Eddú Meléndez | ||||
|  */ | ||||
| class DataElasticsearchTypeExcludeFilter | ||||
| 		extends StandardAnnotationCustomizableTypeExcludeFilter<DataElasticsearchTest> { | ||||
| 
 | ||||
| 	protected DataElasticsearchTypeExcludeFilter(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 Elasticsearch tests. | ||||
|  */ | ||||
| package org.springframework.boot.test.autoconfigure.data.elasticsearch; | ||||
|  | @ -0,0 +1,6 @@ | |||
| # AutoConfigureDataElasticsearch auto-configuration imports | ||||
| org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration | ||||
| org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration | ||||
| org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration | ||||
| org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRestClientAutoConfiguration | ||||
| org.springframework.boot.autoconfigure.data.elasticsearch.ReactiveElasticsearchRepositoriesAutoConfiguration | ||||
|  | @ -0,0 +1,85 @@ | |||
| /* | ||||
|  * 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.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.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.elasticsearch.core.ElasticsearchRestTemplate; | ||||
| 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; | ||||
| 
 | ||||
| /** | ||||
|  * Sample test for {@link DataElasticsearchTest @DataElasticsearchTest} | ||||
|  * | ||||
|  * @author Eddú Meléndez | ||||
|  */ | ||||
| @DataElasticsearchTest | ||||
| @Testcontainers(disabledWithoutDocker = true) | ||||
| class DataElasticsearchTestIntegrationTests { | ||||
| 
 | ||||
| 	@Container | ||||
| 	static final ElasticsearchContainer elasticsearch = new ElasticsearchContainer(DockerImageNames.elasticsearch()) | ||||
| 			.withStartupAttempts(5).withStartupTimeout(Duration.ofMinutes(10)); | ||||
| 
 | ||||
| 	@DynamicPropertySource | ||||
| 	static void elasticsearchProperties(DynamicPropertyRegistry registry) { | ||||
| 		registry.add("spring.elasticsearch.uris", elasticsearch::getHttpHostAddress); | ||||
| 	} | ||||
| 
 | ||||
| 	@Autowired | ||||
| 	private ElasticsearchRestTemplate elasticsearchRestTemplate; | ||||
| 
 | ||||
| 	@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 @DataElasticsearchTest!"); | ||||
| 		String id = UUID.randomUUID().toString(); | ||||
| 		document.setId(id); | ||||
| 		ExampleDocument savedDocument = this.exampleRepository.save(document); | ||||
| 		ExampleDocument getDocument = this.elasticsearchRestTemplate.get(id, ExampleDocument.class); | ||||
| 		assertThat(getDocument).isNotNull(); | ||||
| 		assertThat(getDocument.getId()).isNotNull(); | ||||
| 		assertThat(getDocument.getId()).isEqualTo(savedDocument.getId()); | ||||
| 		this.exampleRepository.deleteAll(); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,61 @@ | |||
| /* | ||||
|  * 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.elasticsearch; | ||||
| 
 | ||||
| import java.time.Duration; | ||||
| 
 | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.testcontainers.elasticsearch.ElasticsearchContainer; | ||||
| 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.core.env.Environment; | ||||
| import org.springframework.test.context.DynamicPropertyRegistry; | ||||
| import org.springframework.test.context.DynamicPropertySource; | ||||
| 
 | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
| 
 | ||||
| /** | ||||
|  * Tests for the {@link DataElasticsearchTest#properties properties} attribute of | ||||
|  * {@link DataElasticsearchTest @DataElasticsearchTest}. | ||||
|  * | ||||
|  * @author Eddú Meléndez | ||||
|  */ | ||||
| @DataElasticsearchTest(properties = "spring.profiles.active=test") | ||||
| @Testcontainers(disabledWithoutDocker = true) | ||||
| class DataElasticsearchTestPropertiesIntegrationTests { | ||||
| 
 | ||||
| 	@Container | ||||
| 	static final ElasticsearchContainer elasticsearch = new ElasticsearchContainer(DockerImageNames.elasticsearch()) | ||||
| 			.withStartupAttempts(5).withStartupTimeout(Duration.ofMinutes(10)); | ||||
| 
 | ||||
| 	@DynamicPropertySource | ||||
| 	static void elasticsearchProperties(DynamicPropertyRegistry registry) { | ||||
| 		registry.add("spring.elasticsearch.uris", elasticsearch::getHttpHostAddress); | ||||
| 	} | ||||
| 
 | ||||
| 	@Autowired | ||||
| 	private Environment environment; | ||||
| 
 | ||||
| 	@Test | ||||
| 	void environmentWithNewProfile() { | ||||
| 		assertThat(this.environment.getActiveProfiles()).containsExactly("test"); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,69 @@ | |||
| /* | ||||
|  * 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.elasticsearch; | ||||
| 
 | ||||
| import java.time.Duration; | ||||
| 
 | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.testcontainers.elasticsearch.ElasticsearchContainer; | ||||
| 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.data.elasticsearch.core.ReactiveElasticsearchTemplate; | ||||
| import org.springframework.test.context.DynamicPropertyRegistry; | ||||
| import org.springframework.test.context.DynamicPropertySource; | ||||
| 
 | ||||
| import static org.assertj.core.api.Assertions.assertThat; | ||||
| 
 | ||||
| /** | ||||
|  * Sample tests for {@link DataElasticsearchTest @DataElasticsearchTest} using reactive | ||||
|  * repositories. | ||||
|  * | ||||
|  * @author Eddú Meléndez | ||||
|  */ | ||||
| @DataElasticsearchTest | ||||
| @Testcontainers(disabledWithoutDocker = true) | ||||
| class DataElasticsearchTestReactiveIntegrationTests { | ||||
| 
 | ||||
| 	@Container | ||||
| 	static final ElasticsearchContainer elasticsearch = new ElasticsearchContainer(DockerImageNames.elasticsearch()) | ||||
| 			.withStartupAttempts(5).withStartupTimeout(Duration.ofMinutes(10)); | ||||
| 
 | ||||
| 	@DynamicPropertySource | ||||
| 	static void elasticsearchProperties(DynamicPropertyRegistry registry) { | ||||
| 		registry.add("spring.elasticsearch.uris", elasticsearch::getHttpHostAddress); | ||||
| 	} | ||||
| 
 | ||||
| 	@Autowired | ||||
| 	private ReactiveElasticsearchTemplate elasticsearchTemplate; | ||||
| 
 | ||||
| 	@Autowired | ||||
| 	private ExampleReactiveRepository exampleReactiveRepository; | ||||
| 
 | ||||
| 	@Test | ||||
| 	void testRepository() { | ||||
| 		ExampleDocument exampleDocument = new ExampleDocument(); | ||||
| 		exampleDocument.setText("Look, new @DataElasticsearchTest!"); | ||||
| 		exampleDocument = this.exampleReactiveRepository.save(exampleDocument).block(Duration.ofSeconds(30)); | ||||
| 		assertThat(exampleDocument.getId()).isNotNull(); | ||||
| 		assertThat(this.elasticsearchTemplate.exists(exampleDocument.getId(), ExampleDocument.class) | ||||
| 				.block(Duration.ofSeconds(30))).isTrue(); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,71 @@ | |||
| /* | ||||
|  * 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.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.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 DataElasticsearchTest @DataElasticsearchTest}. | ||||
|  * | ||||
|  * @author Eddú Meléndez | ||||
|  */ | ||||
| @DataElasticsearchTest(includeFilters = @Filter(Service.class)) | ||||
| @Testcontainers(disabledWithoutDocker = true) | ||||
| class DataElasticsearchTestWithIncludeFilterIntegrationTests { | ||||
| 
 | ||||
| 	@Container | ||||
| 	static final ElasticsearchContainer elasticsearch = new ElasticsearchContainer(DockerImageNames.elasticsearch()) | ||||
| 			.withStartupAttempts(5).withStartupTimeout(Duration.ofMinutes(10)); | ||||
| 
 | ||||
| 	@DynamicPropertySource | ||||
| 	static void elasticsearchProperties(DynamicPropertyRegistry registry) { | ||||
| 		registry.add("spring.elasticsearch.uris", elasticsearch::getHttpHostAddress); | ||||
| 	} | ||||
| 
 | ||||
| 	@Autowired | ||||
| 	private ExampleRepository exampleRepository; | ||||
| 
 | ||||
| 	@Autowired | ||||
| 	private ExampleService service; | ||||
| 
 | ||||
| 	@Test | ||||
| 	void testService() { | ||||
| 		ExampleDocument document = new ExampleDocument(); | ||||
| 		document.setText("Look, new @DataElasticsearchTest!"); | ||||
| 		String id = UUID.randomUUID().toString(); | ||||
| 		document.setId(id); | ||||
| 		ExampleDocument savedDocument = this.exampleRepository.save(document); | ||||
| 		assertThat(this.service.findById(savedDocument.getId())).isNotNull(); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,51 @@ | |||
| /* | ||||
|  * 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.elasticsearch; | ||||
| 
 | ||||
| import org.springframework.data.annotation.Id; | ||||
| import org.springframework.data.elasticsearch.annotations.Document; | ||||
| 
 | ||||
| /** | ||||
|  * Example document used with {@link DataElasticsearchTest @DataElasticsearchTest} tests. | ||||
|  * | ||||
|  * @author Eddú Meléndez | ||||
|  */ | ||||
| @Document(indexName = "examples") | ||||
| public class ExampleDocument { | ||||
| 
 | ||||
| 	@Id | ||||
| 	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,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.elasticsearch; | ||||
| 
 | ||||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||||
| 
 | ||||
| /** | ||||
|  * Example {@link SpringBootApplication @SpringBootApplication} used with | ||||
|  * {@link DataElasticsearchTest @DataElasticsearchTest} tests. | ||||
|  * | ||||
|  * @author Eddú Meléndez | ||||
|  */ | ||||
| @SpringBootApplication | ||||
| public class ExampleElasticsearchApplication { | ||||
| 
 | ||||
| } | ||||
|  | @ -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.elasticsearch; | ||||
| 
 | ||||
| import org.springframework.data.elasticsearch.repository.ReactiveElasticsearchRepository; | ||||
| 
 | ||||
| /** | ||||
|  * Example reactive repository used with | ||||
|  * {@link DataElasticsearchTest @DataElasticsearchTest} tests. | ||||
|  * | ||||
|  * @author Eddú Meléndez | ||||
|  */ | ||||
| interface ExampleReactiveRepository extends ReactiveElasticsearchRepository<ExampleDocument, String> { | ||||
| 
 | ||||
| } | ||||
|  | @ -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.elasticsearch; | ||||
| 
 | ||||
| import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; | ||||
| 
 | ||||
| /** | ||||
|  * Example repository used with {@link DataElasticsearchTest @DataElasticsearchTest} | ||||
|  * tests. | ||||
|  * | ||||
|  * @author Eddú Meléndez | ||||
|  */ | ||||
| interface ExampleRepository extends ElasticsearchRepository<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.elasticsearch; | ||||
| 
 | ||||
| import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; | ||||
| import org.springframework.stereotype.Service; | ||||
| 
 | ||||
| /** | ||||
|  * Example service used with {@link DataElasticsearchTest @DataElasticsearchTest} tests. | ||||
|  * | ||||
|  * @author Eddú Meléndez | ||||
|  */ | ||||
| @Service | ||||
| public class ExampleService { | ||||
| 
 | ||||
| 	private final ElasticsearchRestTemplate elasticsearchRestTemplate; | ||||
| 
 | ||||
| 	public ExampleService(ElasticsearchRestTemplate elasticsearchRestTemplate) { | ||||
| 		this.elasticsearchRestTemplate = elasticsearchRestTemplate; | ||||
| 	} | ||||
| 
 | ||||
| 	public ExampleDocument findById(String id) { | ||||
| 		return this.elasticsearchRestTemplate.get(id, ExampleDocument.class); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
		Loading…
	
		Reference in New Issue