Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4c57437

Browse files
committedFeb 19, 2025·
Introduce TestSliceTestContextBootstrapper for test slices
Signed-off-by: Yanming Zhou <[email protected]>
2 parents 5a99bc3 + edb1703 commit 4c57437

20 files changed

+130
-199
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.cassandra;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
@@ -26,13 +25,6 @@
2625
*
2726
* @author Artsiom Yudovin
2827
*/
29-
class DataCassandraTestContextBootstrapper extends SpringBootTestContextBootstrapper {
30-
31-
@Override
32-
protected String[] getProperties(Class<?> testClass) {
33-
DataCassandraTest dataCassandraTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
34-
DataCassandraTest.class);
35-
return (dataCassandraTest != null) ? dataCassandraTest.properties() : null;
36-
}
28+
class DataCassandraTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataCassandraTest> {
3729

3830
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.couchbase;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
@@ -26,13 +25,6 @@
2625
*
2726
* @author Eddú Meléndez
2827
*/
29-
class DataCouchbaseTestContextBootstrapper extends SpringBootTestContextBootstrapper {
30-
31-
@Override
32-
protected String[] getProperties(Class<?> testClass) {
33-
DataCouchbaseTest dataCouchbaseTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
34-
DataCouchbaseTest.class);
35-
return (dataCouchbaseTest != null) ? dataCouchbaseTest.properties() : null;
36-
}
28+
class DataCouchbaseTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataCouchbaseTest> {
3729

3830
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.elasticsearch;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
@@ -26,13 +25,6 @@
2625
*
2726
* @author Eddú Meléndez
2827
*/
29-
class DataElasticsearchTestContextBootstrapper extends SpringBootTestContextBootstrapper {
30-
31-
@Override
32-
protected String[] getProperties(Class<?> testClass) {
33-
DataElasticsearchTest dataElasticsearchTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
34-
DataElasticsearchTest.class);
35-
return (dataElasticsearchTest != null) ? dataElasticsearchTest.properties() : null;
36-
}
28+
class DataElasticsearchTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataElasticsearchTest> {
3729

3830
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.jdbc;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link DataJdbcTest @DataJdbcTest} support.
2524
*
2625
* @author Andy Wilkinson
2726
*/
28-
class DataJdbcTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
DataJdbcTest dataJdbcTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, DataJdbcTest.class);
33-
return (dataJdbcTest != null) ? dataJdbcTest.properties() : null;
34-
}
27+
class DataJdbcTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataJdbcTest> {
3528

3629
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.ldap;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link DataLdapTest @DataLdapTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class DataLdapTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
DataLdapTest dataLdapTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, DataLdapTest.class);
33-
return (dataLdapTest != null) ? dataLdapTest.properties() : null;
34-
}
27+
class DataLdapTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataLdapTest> {
3528

3629
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.mongo;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link DataMongoTest @DataMongoTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class DataMongoTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
DataMongoTest dataMongoTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, DataMongoTest.class);
33-
return (dataMongoTest != null) ? dataMongoTest.properties() : null;
34-
}
27+
class DataMongoTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataMongoTest> {
3528

3629
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.neo4j;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link DataNeo4jTest @DataNeo4jTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class DataNeo4jTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
DataNeo4jTest dataNeo4jTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, DataNeo4jTest.class);
33-
return (dataNeo4jTest != null) ? dataNeo4jTest.properties() : null;
34-
}
27+
class DataNeo4jTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataNeo4jTest> {
3528

3629
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.r2dbc;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link DataR2dbcTest @DataR2dbcTest} support.
2524
*
2625
* @author Mark Paluch
2726
*/
28-
class DataR2dbcTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
DataR2dbcTest dataR2dbcTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, DataR2dbcTest.class);
33-
return (dataR2dbcTest != null) ? dataR2dbcTest.properties() : null;
34-
}
27+
class DataR2dbcTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataR2dbcTest> {
3528

3629
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.data.redis;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link DataRedisTest @DataRedisTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class DataRedisTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
DataRedisTest dataRedisTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, DataRedisTest.class);
33-
return (dataRedisTest != null) ? dataRedisTest.properties() : null;
34-
}
27+
class DataRedisTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataRedisTest> {
3528

3629
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 the original author or authors.
2+
* Copyright 2020-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.graphql;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link GraphQlTest @GraphQlTest}.
2524
*
2625
* @author Brian Clozel
2726
*/
28-
class GraphQlTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
GraphQlTest graphQlTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, GraphQlTest.class);
33-
return (graphQlTest != null) ? graphQlTest.properties() : null;
34-
}
27+
class GraphQlTestContextBootstrapper extends TestSliceTestContextBootstrapper<GraphQlTest> {
3528

3629
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,15 @@
1616

1717
package org.springframework.boot.test.autoconfigure.jdbc;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link JdbcTest @JdbcTest} support.
2524
*
2625
* @author Artsiom Yudovin
26+
* @author Yanming Zhou
2727
*/
28-
class JdbcTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
JdbcTest jdbcTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, JdbcTest.class);
33-
return (jdbcTest != null) ? jdbcTest.properties() : null;
34-
}
28+
class JdbcTestContextBootstrapper extends TestSliceTestContextBootstrapper<JdbcTest> {
3529

3630
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.jooq;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link JooqTest @JooqTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class JooqTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
JooqTest jooqTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, JooqTest.class);
33-
return (jooqTest != null) ? jooqTest.properties() : null;
34-
}
27+
class JooqTestContextBootstrapper extends TestSliceTestContextBootstrapper<JooqTest> {
3528

3629
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.json;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link JsonTest @JsonTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class JsonTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
JsonTest jsonTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, JsonTest.class);
33-
return (jsonTest != null) ? jsonTest.properties() : null;
34-
}
27+
class JsonTestContextBootstrapper extends TestSliceTestContextBootstrapper<JsonTest> {
3528

3629
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,24 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.orm.jpa;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.core.annotation.MergedAnnotations;
21-
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2220
import org.springframework.test.context.TestContextBootstrapper;
2321

2422
/**
2523
* {@link TestContextBootstrapper} for {@link DataJpaTest @DataJpaTest} support.
2624
*
2725
* @author Artsiom Yudovin
2826
*/
29-
class DataJpaTestContextBootstrapper extends SpringBootTestContextBootstrapper {
30-
31-
@Override
32-
protected String[] getProperties(Class<?> testClass) {
33-
return MergedAnnotations.from(testClass, SearchStrategy.INHERITED_ANNOTATIONS)
34-
.get(DataJpaTest.class)
35-
.getValue("properties", String[].class)
36-
.orElse(null);
37-
}
27+
class DataJpaTestContextBootstrapper extends TestSliceTestContextBootstrapper<DataJpaTest> {
3828

3929
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,22 +16,14 @@
1616

1717
package org.springframework.boot.test.autoconfigure.web.client;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
2423
* {@link TestContextBootstrapper} for {@link RestClientTest @RestClientTest} support.
2524
*
2625
* @author Artsiom Yudovin
2726
*/
28-
class RestClientTestContextBootstrapper extends SpringBootTestContextBootstrapper {
29-
30-
@Override
31-
protected String[] getProperties(Class<?> testClass) {
32-
RestClientTest restClientTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
33-
RestClientTest.class);
34-
return (restClientTest != null) ? restClientTest.properties() : null;
35-
}
27+
class RestClientTestContextBootstrapper extends TestSliceTestContextBootstrapper<RestClientTest> {
3628

3729
}

‎spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/reactive/WebFluxTestContextBootstrapper.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,9 +17,8 @@
1717
package org.springframework.boot.test.autoconfigure.web.reactive;
1818

1919
import org.springframework.boot.test.context.ReactiveWebMergedContextConfiguration;
20-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2121
import org.springframework.test.context.MergedContextConfiguration;
22-
import org.springframework.test.context.TestContextAnnotationUtils;
2322
import org.springframework.test.context.TestContextBootstrapper;
2423

2524
/**
@@ -28,17 +27,11 @@
2827
* @author Stephane Nicoll
2928
* @author Artsiom Yudovin
3029
*/
31-
class WebFluxTestContextBootstrapper extends SpringBootTestContextBootstrapper {
30+
class WebFluxTestContextBootstrapper extends TestSliceTestContextBootstrapper<WebFluxTest> {
3231

3332
@Override
3433
protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
3534
return new ReactiveWebMergedContextConfiguration(super.processMergedContextConfiguration(mergedConfig));
3635
}
3736

38-
@Override
39-
protected String[] getProperties(Class<?> testClass) {
40-
WebFluxTest webFluxTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, WebFluxTest.class);
41-
return (webFluxTest != null) ? webFluxTest.properties() : null;
42-
}
43-
4437
}

‎spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTestContextBootstrapper.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,9 +16,8 @@
1616

1717
package org.springframework.boot.test.autoconfigure.web.servlet;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2020
import org.springframework.test.context.MergedContextConfiguration;
21-
import org.springframework.test.context.TestContextAnnotationUtils;
2221
import org.springframework.test.context.TestContextBootstrapper;
2322
import org.springframework.test.context.web.WebMergedContextConfiguration;
2423

@@ -29,18 +28,12 @@
2928
* @author Artsiom Yudovin
3029
* @author Lorenzo Dee
3130
*/
32-
class WebMvcTestContextBootstrapper extends SpringBootTestContextBootstrapper {
31+
class WebMvcTestContextBootstrapper extends TestSliceTestContextBootstrapper<WebMvcTest> {
3332

3433
@Override
3534
protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
3635
MergedContextConfiguration processedMergedConfiguration = super.processMergedContextConfiguration(mergedConfig);
3736
return new WebMergedContextConfiguration(processedMergedConfiguration, determineResourceBasePath(mergedConfig));
3837
}
3938

40-
@Override
41-
protected String[] getProperties(Class<?> testClass) {
42-
WebMvcTest webMvcTest = TestContextAnnotationUtils.findMergedAnnotation(testClass, WebMvcTest.class);
43-
return (webMvcTest != null) ? webMvcTest.properties() : null;
44-
}
45-
4639
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.boot.test.autoconfigure.webservices.client;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
20-
import org.springframework.test.context.TestContextAnnotationUtils;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2120
import org.springframework.test.context.TestContextBootstrapper;
2221

2322
/**
@@ -26,13 +25,6 @@
2625
*
2726
* @author Dmytro Nosan
2827
*/
29-
class WebServiceClientTestContextBootstrapper extends SpringBootTestContextBootstrapper {
30-
31-
@Override
32-
protected String[] getProperties(Class<?> testClass) {
33-
WebServiceClientTest webServiceClientTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
34-
WebServiceClientTest.class);
35-
return (webServiceClientTest != null) ? webServiceClientTest.properties() : null;
36-
}
28+
class WebServiceClientTestContextBootstrapper extends TestSliceTestContextBootstrapper<WebServiceClientTest> {
3729

3830
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,9 +16,8 @@
1616

1717
package org.springframework.boot.test.autoconfigure.webservices.server;
1818

19-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
19+
import org.springframework.boot.test.context.TestSliceTestContextBootstrapper;
2020
import org.springframework.test.context.MergedContextConfiguration;
21-
import org.springframework.test.context.TestContextAnnotationUtils;
2221
import org.springframework.test.context.TestContextBootstrapper;
2322
import org.springframework.test.context.web.WebMergedContextConfiguration;
2423

@@ -28,19 +27,12 @@
2827
*
2928
* @author Daniil Razorenov
3029
*/
31-
class WebServiceServerTestContextBootstrapper extends SpringBootTestContextBootstrapper {
30+
class WebServiceServerTestContextBootstrapper extends TestSliceTestContextBootstrapper<WebServiceServerTest> {
3231

3332
@Override
3433
protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
3534
MergedContextConfiguration processedMergedConfiguration = super.processMergedContextConfiguration(mergedConfig);
3635
return new WebMergedContextConfiguration(processedMergedConfiguration, determineResourceBasePath(mergedConfig));
3736
}
3837

39-
@Override
40-
protected String[] getProperties(Class<?> testClass) {
41-
WebServiceServerTest webServiceServerTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
42-
WebServiceServerTest.class);
43-
return (webServiceServerTest != null) ? webServiceServerTest.properties() : null;
44-
}
45-
4638
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2012-2025 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.context;
18+
19+
import java.lang.annotation.Annotation;
20+
import java.lang.reflect.Method;
21+
22+
import org.springframework.core.ResolvableType;
23+
import org.springframework.test.context.TestContextAnnotationUtils;
24+
import org.springframework.test.context.TestContextBootstrapper;
25+
import org.springframework.util.Assert;
26+
27+
/**
28+
* {@link TestContextBootstrapper} for test slices support.
29+
*
30+
* @param <T> the test slice annotation
31+
* @author Yanming Zhou
32+
* @since 3.5.0
33+
*/
34+
public class TestSliceTestContextBootstrapper<T extends Annotation> extends SpringBootTestContextBootstrapper {
35+
36+
private final Class<T> annotationType;
37+
38+
private final Method propertiesMethod;
39+
40+
@SuppressWarnings("unchecked")
41+
public TestSliceTestContextBootstrapper() {
42+
this.annotationType = (Class<T>) ResolvableType.forClass(getClass())
43+
.as(TestSliceTestContextBootstrapper.class)
44+
.getGeneric(0)
45+
.resolve();
46+
Assert.notNull(this.annotationType, "'%s' doesn't contain type parameter of '%s'"
47+
.formatted(getClass().getName(), TestSliceTestContextBootstrapper.class.getName()));
48+
try {
49+
this.propertiesMethod = this.annotationType.getDeclaredMethod("properties");
50+
Assert.state(this.propertiesMethod.getReturnType() == String[].class,
51+
"Method '%s.properties()' should return String[]".formatted(this.annotationType.getName()));
52+
}
53+
catch (NoSuchMethodException ex) {
54+
throw new RuntimeException(
55+
"Missing method '%s.properties()', consider switching to extends from '%s' directly".formatted(
56+
this.annotationType.getName(), SpringBootTestContextBootstrapper.class.getName()),
57+
ex);
58+
}
59+
}
60+
61+
@Override
62+
protected String[] getProperties(Class<?> testClass) {
63+
Annotation annotation = TestContextAnnotationUtils.findMergedAnnotation(testClass, this.annotationType);
64+
try {
65+
return (annotation != null) ? (String[]) this.propertiesMethod.invoke(annotation) : null;
66+
}
67+
catch (Exception ex) {
68+
throw new RuntimeException("It shouldn't happen");
69+
}
70+
}
71+
72+
}

0 commit comments

Comments
 (0)
Please sign in to comment.