Skip to content

Commit 55c410a

Browse files
committed
Improve autoconfigure's use of test resources
Closes gh-44512
1 parent 1f5b9de commit 55c410a

File tree

181 files changed

+1655
-820
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+1655
-820
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/AutoConfigurationMetadataLoaderTests.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 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.
@@ -20,13 +20,21 @@
2020

2121
import org.junit.jupiter.api.Test;
2222

23+
import org.springframework.boot.testsupport.classpath.resources.WithResource;
24+
2325
import static org.assertj.core.api.Assertions.assertThat;
2426

2527
/**
2628
* Test for {@link AutoConfigurationMetadataLoader}.
2729
*
2830
* @author Phillip Webb
2931
*/
32+
@WithResource(name = "metadata.properties", content = """
33+
test=
34+
test.string=abc
35+
test.int=123
36+
test.set=a,b,b,c
37+
""")
3038
class AutoConfigurationMetadataLoaderTests {
3139

3240
@Test
@@ -90,8 +98,8 @@ void getWithDefaultWhenMissingShouldReturnDefault() {
9098
}
9199

92100
private AutoConfigurationMetadata load() {
93-
return AutoConfigurationMetadataLoader.loadMetadata(null,
94-
"META-INF/AutoConfigurationMetadataLoaderTests.properties");
101+
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
102+
return AutoConfigurationMetadataLoader.loadMetadata(classLoader, "metadata.properties");
95103
}
96104

97105
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ImportAutoConfigurationImportSelectorTests.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
3030
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
31+
import org.springframework.boot.testsupport.classpath.resources.WithResource;
3132
import org.springframework.core.annotation.AliasFor;
3233
import org.springframework.core.io.DefaultResourceLoader;
3334
import org.springframework.core.type.AnnotationMetadata;
@@ -74,6 +75,12 @@ void importsAreSelectedUsingClassesAttribute() throws Exception {
7475
}
7576

7677
@Test
78+
@WithResource(
79+
name = "META-INF/spring/org.springframework.boot.autoconfigure.ImportAutoConfigurationImportSelectorTests$FromImportsFile.imports",
80+
content = """
81+
org.springframework.boot.autoconfigure.ImportAutoConfigurationImportSelectorTests$ImportedAutoConfiguration
82+
org.springframework.boot.autoconfigure.missing.MissingAutoConfiguration
83+
""")
7784
void importsAreSelectedFromImportsFile() throws Exception {
7885
AnnotationMetadata annotationMetadata = getAnnotationMetadata(FromImportsFile.class);
7986
String[] imports = this.importSelector.selectImports(annotationMetadata);
@@ -83,9 +90,15 @@ void importsAreSelectedFromImportsFile() throws Exception {
8390
}
8491

8592
@Test
93+
@WithResource(
94+
name = "META-INF/spring/org.springframework.boot.autoconfigure.ImportAutoConfigurationImportSelectorTests$FromImportsFile.imports",
95+
content = """
96+
optional:org.springframework.boot.autoconfigure.ImportAutoConfigurationImportSelectorTests$ImportedAutoConfiguration
97+
optional:org.springframework.boot.autoconfigure.missing.MissingAutoConfiguration
98+
org.springframework.boot.autoconfigure.ImportAutoConfigurationImportSelectorTests$AnotherImportedAutoConfiguration
99+
""")
86100
void importsSelectedFromImportsFileIgnoreMissingOptionalClasses() throws Exception {
87-
AnnotationMetadata annotationMetadata = getAnnotationMetadata(
88-
FromImportsFileIgnoresMissingOptionalClasses.class);
101+
AnnotationMetadata annotationMetadata = getAnnotationMetadata(FromImportsFile.class);
89102
String[] imports = this.importSelector.selectImports(annotationMetadata);
90103
assertThat(imports).containsExactly(
91104
"org.springframework.boot.autoconfigure.ImportAutoConfigurationImportSelectorTests$ImportedAutoConfiguration",

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 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.
@@ -910,9 +910,8 @@ void enableSslWithInvalidTrustStoreTypeShouldFail() {
910910
void enableSslWithBundle() {
911911
this.contextRunner.withUserConfiguration(TestConfiguration.class)
912912
.withPropertyValues("spring.rabbitmq.ssl.bundle=test-bundle",
913-
"spring.ssl.bundle.jks.test-bundle.keystore.location=classpath:test.jks",
914-
"spring.ssl.bundle.jks.test-bundle.keystore.password=secret",
915-
"spring.ssl.bundle.jks.test-bundle.key.password=password")
913+
"spring.ssl.bundle.jks.test-bundle.keystore.location=classpath:org/springframework/boot/autoconfigure/amqp/test.jks",
914+
"spring.ssl.bundle.jks.test-bundle.keystore.password=secret")
916915
.run((context) -> {
917916
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context);
918917
assertThat(rabbitConnectionFactory.isSSL()).isTrue();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 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.
@@ -72,6 +72,8 @@
7272
import org.springframework.boot.test.context.FilteredClassLoader;
7373
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
7474
import org.springframework.boot.test.system.OutputCaptureExtension;
75+
import org.springframework.boot.testsupport.classpath.resources.WithPackageResources;
76+
import org.springframework.boot.testsupport.classpath.resources.WithResource;
7577
import org.springframework.context.ApplicationContext;
7678
import org.springframework.context.annotation.Bean;
7779
import org.springframework.context.annotation.Configuration;
@@ -284,13 +286,13 @@ void testUsingJpa() {
284286
}
285287

286288
@Test
289+
@WithPackageResources("custom-schema.sql")
287290
void testRenamePrefix() {
288291
this.contextRunner
289292
.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class,
290293
HibernateJpaAutoConfiguration.class)
291294
.withPropertyValues("spring.datasource.generate-unique-name=true",
292-
"spring.batch.jdbc.schema:classpath:batch/custom-schema.sql",
293-
"spring.batch.jdbc.tablePrefix:PREFIX_")
295+
"spring.batch.jdbc.schema:classpath:custom-schema.sql", "spring.batch.jdbc.tablePrefix:PREFIX_")
294296
.run((context) -> {
295297
assertThat(context).hasSingleBean(JobLauncher.class);
296298
assertThat(context.getBean(BatchProperties.class).getJdbc().getInitializeSchema())
@@ -392,6 +394,7 @@ void jobRepositoryBeansDependOnFlyway() {
392394
}
393395

394396
@Test
397+
@WithResource(name = "db/changelog/db.changelog-master.yaml", content = "databaseChangeLog:")
395398
void jobRepositoryBeansDependOnLiquibase() {
396399
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
397400
.withUserConfiguration(LiquibaseAutoConfiguration.class)

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationWithoutJpaTests.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 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.
@@ -34,6 +34,7 @@
3434
import org.springframework.boot.sql.init.DatabaseInitializationMode;
3535
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
3636
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
37+
import org.springframework.boot.testsupport.classpath.resources.WithPackageResources;
3738
import org.springframework.jdbc.core.JdbcTemplate;
3839
import org.springframework.transaction.annotation.Isolation;
3940

@@ -70,11 +71,11 @@ void jdbcWithDefaultSettings() {
7071
}
7172

7273
@Test
74+
@WithPackageResources("custom-schema.sql")
7375
void jdbcWithCustomPrefix() {
7476
this.contextRunner.withUserConfiguration(DefaultConfiguration.class, EmbeddedDataSourceConfiguration.class)
7577
.withPropertyValues("spring.datasource.generate-unique-name=true",
76-
"spring.batch.jdbc.schema:classpath:batch/custom-schema.sql",
77-
"spring.batch.jdbc.tablePrefix:PREFIX_")
78+
"spring.batch.jdbc.schema:classpath:custom-schema.sql", "spring.batch.jdbc.tablePrefix:PREFIX_")
7879
.run((context) -> {
7980
assertThat(new JdbcTemplate(context.getBean(DataSource.class))
8081
.queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java

+74-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 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,6 +16,10 @@
1616

1717
package org.springframework.boot.autoconfigure.cache;
1818

19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
1923
import java.util.ArrayList;
2024
import java.util.Collections;
2125
import java.util.List;
@@ -47,6 +51,7 @@
4751
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
4852
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
4953
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
54+
import org.springframework.boot.testsupport.classpath.resources.WithResource;
5055
import org.springframework.cache.Cache;
5156
import org.springframework.cache.Cache.ValueWrapper;
5257
import org.springframework.cache.CacheManager;
@@ -461,6 +466,7 @@ void jCacheCacheUseBeanClassLoader() {
461466
}
462467

463468
@Test
469+
@WithHazelcastXmlResource
464470
void hazelcastCacheExplicit() {
465471
this.contextRunner.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class))
466472
.withUserConfiguration(DefaultCacheConfiguration.class)
@@ -476,6 +482,7 @@ void hazelcastCacheExplicit() {
476482
}
477483

478484
@Test
485+
@WithHazelcastXmlResource
479486
void hazelcastCacheWithCustomizers() {
480487
this.contextRunner.withUserConfiguration(HazelcastCacheAndCustomizersConfiguration.class)
481488
.withPropertyValues("spring.cache.type=hazelcast")
@@ -528,10 +535,31 @@ void hazelcastAsJCacheWithCaches() {
528535
}
529536

530537
@Test
538+
@WithResource(name = "hazelcast-specific.xml", content = """
539+
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-5.0.xsd"
540+
xmlns="http://www.hazelcast.com/schema/config"
541+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
542+
543+
<queue name="foobar"/>
544+
545+
<map name="foobar">
546+
<time-to-live-seconds>3600</time-to-live-seconds>
547+
<max-idle-seconds>600</max-idle-seconds>
548+
</map>
549+
550+
<network>
551+
<join>
552+
<auto-detection enabled="false" />
553+
<multicast enabled="false"/>
554+
</join>
555+
</network>
556+
557+
</hazelcast>
558+
""")
531559
void hazelcastAsJCacheWithConfig() {
532560
String cachingProviderFqn = HazelcastServerCachingProvider.class.getName();
533561
try {
534-
String configLocation = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
562+
String configLocation = "hazelcast-specific.xml";
535563
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
536564
.withPropertyValues("spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn,
537565
"spring.cache.jcache.config=" + configLocation)
@@ -548,6 +576,7 @@ void hazelcastAsJCacheWithConfig() {
548576
}
549577

550578
@Test
579+
@WithHazelcastXmlResource
551580
void hazelcastAsJCacheWithExistingHazelcastInstance() {
552581
String cachingProviderFqn = HazelcastServerCachingProvider.class.getName();
553582
this.contextRunner.withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class))
@@ -567,6 +596,7 @@ void hazelcastAsJCacheWithExistingHazelcastInstance() {
567596
}
568597

569598
@Test
599+
@WithInfinispanXmlResource
570600
void infinispanCacheWithConfig() {
571601
this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class)
572602
.withPropertyValues("spring.cache.type=infinispan", "spring.cache.infinispan.config=infinispan.xml")
@@ -615,6 +645,7 @@ void infinispanAsJCacheWithCaches() {
615645
}
616646

617647
@Test
648+
@WithInfinispanXmlResource
618649
void infinispanAsJCacheWithConfig() {
619650
String cachingProviderClassName = JCachingProvider.class.getName();
620651
String configLocation = "infinispan.xml";
@@ -1058,4 +1089,45 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
10581089

10591090
}
10601091

1092+
@Target(ElementType.METHOD)
1093+
@Retention(RetentionPolicy.RUNTIME)
1094+
@WithResource(name = "infinispan.xml", content = """
1095+
<?xml version="1.0" encoding="UTF-8"?>
1096+
<infinispan>
1097+
1098+
<!-- ************************************** -->
1099+
<!-- Corresponds to @Cacheable("cache-name") -->
1100+
<!-- ************************************** -->
1101+
<cache-container default-cache="default">
1102+
<local-cache name="default"/>
1103+
<local-cache name="foo"/>
1104+
<local-cache name="bar" />
1105+
</cache-container>
1106+
1107+
</infinispan>
1108+
""")
1109+
@interface WithInfinispanXmlResource {
1110+
1111+
}
1112+
1113+
@Target(ElementType.METHOD)
1114+
@Retention(RetentionPolicy.RUNTIME)
1115+
@WithResource(name = "hazelcast.xml", content = """
1116+
<hazelcast
1117+
xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-5.0.xsd"
1118+
xmlns="http://www.hazelcast.com/schema/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1119+
<instance-name>default-instance</instance-name>
1120+
<map name="defaultCache" />
1121+
<network>
1122+
<join>
1123+
<auto-detection enabled="false" />
1124+
<multicast enabled="false" />
1125+
</join>
1126+
</network>
1127+
</hazelcast>
1128+
""")
1129+
@interface WithHazelcastXmlResource {
1130+
1131+
}
1132+
10611133
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/EhCache3CacheAutoConfigurationTests.java

+30-1
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.
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.boot.autoconfigure.cache.CacheAutoConfigurationTests.DefaultCacheConfiguration;
2323
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
24+
import org.springframework.boot.testsupport.classpath.resources.WithResource;
2425
import org.springframework.cache.jcache.JCacheCacheManager;
2526
import org.springframework.core.io.ClassPathResource;
2627
import org.springframework.core.io.Resource;
@@ -49,6 +50,34 @@ void ehcache3AsJCacheWithCaches() {
4950
}
5051

5152
@Test
53+
@WithResource(name = "ehcache3.xml", content = """
54+
<config
55+
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
56+
xmlns='http://www.ehcache.org/v3'
57+
xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
58+
xsi:schemaLocation="
59+
http://www.ehcache.org/v3 https://www.ehcache.org/schema/ehcache-core-3.10.xsd
60+
http://www.ehcache.org/v3/jsr107 https://www.ehcache.org/schema/ehcache-107-ext-3.10.xsd">
61+
62+
<cache-template name="example">
63+
<heap unit="entries">200</heap>
64+
</cache-template>
65+
66+
<cache alias="foo" uses-template="example">
67+
<expiry>
68+
<ttl unit="seconds">600</ttl>
69+
</expiry>
70+
<jsr107:mbeans enable-statistics="true"/>
71+
</cache>
72+
73+
<cache alias="bar" uses-template="example">
74+
<expiry>
75+
<ttl unit="seconds">400</ttl>
76+
</expiry>
77+
</cache>
78+
79+
</config>
80+
""")
5281
void ehcache3AsJCacheWithConfig() {
5382
String cachingProviderFqn = EhcacheCachingProvider.class.getName();
5483
String configLocation = "ehcache3.xml";

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java

+3-1
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.
@@ -38,6 +38,7 @@
3838
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration;
3939
import org.springframework.boot.ssl.NoSuchSslBundleException;
4040
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
41+
import org.springframework.boot.testsupport.classpath.resources.WithPackageResources;
4142
import org.springframework.context.annotation.Bean;
4243
import org.springframework.context.annotation.Configuration;
4344

@@ -89,6 +90,7 @@ void cqlSessionBuilderWithSslEnabled() {
8990
}
9091

9192
@Test
93+
@WithPackageResources("test.jks")
9294
void cqlSessionBuilderWithSslBundle() {
9395
this.contextRunner
9496
.withPropertyValues("spring.cassandra.ssl.bundle=test-bundle",

0 commit comments

Comments
 (0)