Skip to content

Commit b943817

Browse files
committed
Close ApplicationContext after test AOT processing
See commit 1c10805 Closes gh-34841
1 parent 4466548 commit b943817

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

spring-test/src/main/java/org/springframework/test/context/aot/TestContextAotGenerator.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ else if (logger.isWarnEnabled()) {
314314
ClassName processAheadOfTime(MergedContextConfiguration mergedConfig,
315315
GenerationContext generationContext) throws TestContextAotException {
316316

317-
GenericApplicationContext gac = loadContextForAotProcessing(mergedConfig);
318-
try {
317+
try (GenericApplicationContext gac = loadContextForAotProcessing(mergedConfig)) {
319318
return this.aotGenerator.processAheadOfTime(gac, generationContext);
320319
}
321320
catch (Throwable ex) {
@@ -333,7 +332,7 @@ ClassName processAheadOfTime(MergedContextConfiguration mergedConfig,
333332
* context or if one of the prerequisites is not met
334333
* @see AotContextLoader#loadContextForAotProcessing(MergedContextConfiguration, RuntimeHints)
335334
*/
336-
private GenericApplicationContext loadContextForAotProcessing(
335+
GenericApplicationContext loadContextForAotProcessing(
337336
MergedContextConfiguration mergedConfig) throws TestContextAotException {
338337

339338
Class<?> testClass = mergedConfig.getTestClass();

spring-test/src/test/java/org/springframework/test/context/aot/TestContextAotGeneratorTests.java

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-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,13 +16,23 @@
1616

1717
package org.springframework.test.context.aot;
1818

19+
import java.util.ArrayList;
20+
import java.util.List;
21+
import java.util.stream.Stream;
22+
1923
import org.junit.jupiter.api.AfterEach;
2024
import org.junit.jupiter.api.BeforeEach;
2125
import org.junit.jupiter.api.Test;
2226
import org.junit.jupiter.params.ParameterizedTest;
2327
import org.junit.jupiter.params.provider.ValueSource;
2428

29+
import org.springframework.aot.generate.GeneratedFiles;
30+
import org.springframework.aot.generate.InMemoryGeneratedFiles;
31+
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.context.support.GenericApplicationContext;
2533
import org.springframework.core.SpringProperties;
34+
import org.springframework.test.context.MergedContextConfiguration;
35+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2636

2737
import static org.assertj.core.api.Assertions.assertThat;
2838
import static org.springframework.test.context.aot.TestContextAotGenerator.FAIL_ON_ERROR_PROPERTY_NAME;
@@ -60,9 +70,55 @@ void failOnErrorDisabledViaSpringProperty(String value) {
6070
assertThat(createGenerator().failOnError).isFalse();
6171
}
6272

73+
@Test // gh-34841
74+
void contextIsClosedAfterAotProcessing() {
75+
DemoTestContextAotGenerator generator = createGenerator();
76+
generator.processAheadOfTime(Stream.of(TestCase1.class, TestCase2.class));
77+
78+
assertThat(generator.contexts)
79+
.allSatisfy(context -> assertThat(context.isClosed()).as("context is closed").isTrue());
80+
}
81+
82+
83+
private static DemoTestContextAotGenerator createGenerator() {
84+
return new DemoTestContextAotGenerator(new InMemoryGeneratedFiles());
85+
}
86+
87+
88+
private static class DemoTestContextAotGenerator extends TestContextAotGenerator {
89+
90+
List<GenericApplicationContext> contexts = new ArrayList<>();
91+
92+
DemoTestContextAotGenerator(GeneratedFiles generatedFiles) {
93+
super(generatedFiles);
94+
}
95+
96+
@Override
97+
GenericApplicationContext loadContextForAotProcessing(
98+
MergedContextConfiguration mergedConfig) throws TestContextAotException {
99+
100+
GenericApplicationContext context = super.loadContextForAotProcessing(mergedConfig);
101+
this.contexts.add(context);
102+
return context;
103+
}
104+
}
105+
106+
@SpringJUnitConfig
107+
private static class TestCase1 {
108+
109+
@Configuration(proxyBeanMethods = false)
110+
static class Config {
111+
// no beans
112+
}
113+
}
114+
115+
@SpringJUnitConfig
116+
private static class TestCase2 {
63117

64-
private static TestContextAotGenerator createGenerator() {
65-
return new TestContextAotGenerator(null);
118+
@Configuration(proxyBeanMethods = false)
119+
static class Config {
120+
// no beans
121+
}
66122
}
67123

68124
}

0 commit comments

Comments
 (0)