Skip to content

Commit 8015fb0

Browse files
committed
Merge pull request #29911 from eddumelendez
* gh-29911: Polish "Add DataElasticsearchTest annotation" Add DataElasticsearchTest annotation Closes gh-29911
2 parents 9f00c3a + 53292a1 commit 8015fb0

File tree

21 files changed

+828
-0
lines changed

21 files changed

+828
-0
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,22 @@ include::code:MyDataCassandraTests[]
472472

473473

474474

475+
[[features.testing.spring-boot-applications.autoconfigured-spring-data-elasticsearch]]
476+
==== Auto-configured Data Elasticsearch Tests
477+
You can use `@DataElasticsearchTest` to test Elasticsearch applications.
478+
By default, it configures a `ElasticsearchRestTemplate`, scans for `@Document` classes, and configures Spring Data Elasticsearch repositories.
479+
Regular `@Component` and `@ConfigurationProperties` beans are not scanned when the `@DataElasticsearchTest` annotation is used.
480+
`@EnableConfigurationProperties` can be used to include `@ConfigurationProperties` beans.
481+
(For more about using Elasticsearch with Spring Boot, see "<<data#data.nosql.elasticsearch>>", earlier in this chapter.)
482+
483+
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>>.
484+
485+
The following example shows a typical setup for using Elasticsearch tests in Spring Boot:
486+
487+
include::code:MyDataElasticsearchTests[]
488+
489+
490+
475491
[[features.testing.spring-boot-applications.autoconfigured-spring-data-jpa]]
476492
==== Auto-configured Data JPA Tests
477493
You can use the `@DataJpaTest` annotation to test JPA applications.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringdataelasticsearch;
18+
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.boot.test.autoconfigure.data.elasticsearch.DataElasticsearchTest;
21+
22+
@DataElasticsearchTest
23+
public class MyDataElasticsearchTests {
24+
25+
@Autowired
26+
@SuppressWarnings("unused")
27+
private SomeRepository repository;
28+
29+
// ...
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringdataelasticsearch;
18+
19+
public interface SomeRepository {
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringdataelasticsearch
18+
19+
import org.springframework.beans.factory.annotation.Autowired
20+
import org.springframework.boot.test.autoconfigure.data.elasticsearch.DataElasticsearchTest
21+
22+
@DataElasticsearchTest
23+
class MyDataElasticsearchTests(@Autowired val repository: SomeRepository) {
24+
25+
// ...
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredspringdataelasticsearch
18+
19+
interface SomeRepository

spring-boot-project/spring-boot-test-autoconfigure/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ dependencies {
4848
optional("org.springframework.data:spring-data-cassandra") {
4949
exclude group: "org.slf4j", module: "jcl-over-slf4j"
5050
}
51+
optional("org.springframework.data:spring-data-elasticsearch")
5152
optional("org.springframework.data:spring-data-jdbc")
5253
optional("org.springframework.data:spring-data-jpa")
5354
optional("org.springframework.data:spring-data-ldap")
@@ -82,6 +83,7 @@ dependencies {
8283
testImplementation("com.unboundid:unboundid-ldapsdk")
8384
testImplementation("io.lettuce:lettuce-core")
8485
testImplementation("io.micrometer:micrometer-registry-prometheus")
86+
testImplementation("io.projectreactor.netty:reactor-netty-http")
8587
testImplementation("io.projectreactor:reactor-core")
8688
testImplementation("io.projectreactor:reactor-test")
8789
testImplementation("io.r2dbc:r2dbc-h2")
@@ -107,6 +109,7 @@ dependencies {
107109
testImplementation("org.springframework.hateoas:spring-hateoas")
108110
testImplementation("org.springframework.plugin:spring-plugin-core")
109111
testImplementation("org.testcontainers:cassandra")
112+
testImplementation("org.testcontainers:elasticsearch")
110113
testImplementation("org.testcontainers:junit-jupiter")
111114
testImplementation("org.testcontainers:mongodb")
112115
testImplementation("org.testcontainers:neo4j")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.data.elasticsearch;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Inherited;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
27+
28+
/**
29+
* {@link ImportAutoConfiguration Auto-configuration imports} for typical Data
30+
* Elasticsearch tests. Most tests should consider using
31+
* {@link DataElasticsearchTest @DataElasticsearchTest} rather than using this annotation
32+
* directly.
33+
*
34+
* @author Eddú Meléndez
35+
* @since 2.7.0
36+
* @see DataElasticsearchTest
37+
*/
38+
@Target(ElementType.TYPE)
39+
@Retention(RetentionPolicy.RUNTIME)
40+
@Documented
41+
@Inherited
42+
@ImportAutoConfiguration
43+
public @interface AutoConfigureDataElasticsearch {
44+
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.data.elasticsearch;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Inherited;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
import org.junit.jupiter.api.extension.ExtendWith;
27+
28+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
29+
import org.springframework.boot.autoconfigure.SpringBootApplication;
30+
import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration;
31+
import org.springframework.boot.test.autoconfigure.core.AutoConfigureCache;
32+
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
33+
import org.springframework.context.annotation.ComponentScan.Filter;
34+
import org.springframework.core.annotation.AliasFor;
35+
import org.springframework.core.env.Environment;
36+
import org.springframework.test.context.BootstrapWith;
37+
import org.springframework.test.context.junit.jupiter.SpringExtension;
38+
39+
/**
40+
* Annotation that can be used for a Data Elasticsearch test that focuses
41+
* <strong>only</strong> on Data Elasticsearch components.
42+
* <p>
43+
* Using this annotation will disable full auto-configuration and instead apply only
44+
* configuration relevant to Data Elasticsearch tests.
45+
* <p>
46+
* When using JUnit 4, this annotation should be used in combination with
47+
* {@code @RunWith(SpringRunner.class)}.
48+
*
49+
* @author Eddú Meléndez
50+
* @since 2.7.0
51+
*/
52+
@Target(ElementType.TYPE)
53+
@Retention(RetentionPolicy.RUNTIME)
54+
@Documented
55+
@Inherited
56+
@BootstrapWith(DataElasticsearchTestContextBootstrapper.class)
57+
@ExtendWith(SpringExtension.class)
58+
@OverrideAutoConfiguration(enabled = false)
59+
@TypeExcludeFilters(DataElasticsearchTypeExcludeFilter.class)
60+
@AutoConfigureCache
61+
@AutoConfigureDataElasticsearch
62+
@ImportAutoConfiguration
63+
public @interface DataElasticsearchTest {
64+
65+
/**
66+
* Properties in form {@literal key=value} that should be added to the Spring
67+
* {@link Environment} before the test runs.
68+
* @return the properties to add
69+
*/
70+
String[] properties() default {};
71+
72+
/**
73+
* Determines if default filtering should be used with
74+
* {@link SpringBootApplication @SpringBootApplication}. By default no beans are
75+
* included.
76+
* @see #includeFilters()
77+
* @see #excludeFilters()
78+
* @return if default filters should be used
79+
*/
80+
boolean useDefaultFilters() default true;
81+
82+
/**
83+
* A set of include filters which can be used to add otherwise filtered beans to the
84+
* application context.
85+
* @return include filters to apply
86+
*/
87+
Filter[] includeFilters() default {};
88+
89+
/**
90+
* A set of exclude filters which can be used to filter beans that would otherwise be
91+
* added to the application context.
92+
* @return exclude filters to apply
93+
*/
94+
Filter[] excludeFilters() default {};
95+
96+
/**
97+
* Auto-configuration exclusions that should be applied for this test.
98+
* @return auto-configuration exclusions to apply
99+
*/
100+
@AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude")
101+
Class<?>[] excludeAutoConfiguration() default {};
102+
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.data.elasticsearch;
18+
19+
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20+
import org.springframework.core.annotation.MergedAnnotations;
21+
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
22+
import org.springframework.test.context.TestContextBootstrapper;
23+
24+
/**
25+
* {@link TestContextBootstrapper} for
26+
* {@link DataElasticsearchTest @DataElasticsearchTest} support.
27+
*
28+
* @author Eddú Meléndez
29+
*/
30+
class DataElasticsearchTestContextBootstrapper extends SpringBootTestContextBootstrapper {
31+
32+
@Override
33+
protected String[] getProperties(Class<?> testClass) {
34+
return MergedAnnotations.from(testClass, SearchStrategy.INHERITED_ANNOTATIONS).get(DataElasticsearchTest.class)
35+
.getValue("properties", String[].class).orElse(null);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.data.elasticsearch;
18+
19+
import org.springframework.boot.context.TypeExcludeFilter;
20+
import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter;
21+
22+
/**
23+
* {@link TypeExcludeFilter} for {@link DataElasticsearchTest @DataElasticsearchTest}.
24+
*
25+
* @author Eddú Meléndez
26+
*/
27+
class DataElasticsearchTypeExcludeFilter
28+
extends StandardAnnotationCustomizableTypeExcludeFilter<DataElasticsearchTest> {
29+
30+
protected DataElasticsearchTypeExcludeFilter(Class<?> testClass) {
31+
super(testClass);
32+
}
33+
34+
}

0 commit comments

Comments
 (0)