Skip to content

Commit 79b0d71

Browse files
committed
Support the use of @⁠Resource in test classes in AOT mode
Closes gh-31733
1 parent f6b36a6 commit 79b0d71

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

spring-test/src/main/java/org/springframework/test/context/support/DependencyInjectionTestExecutionListener.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -23,6 +23,7 @@
2323
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
2424
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2525
import org.springframework.context.ApplicationContext;
26+
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
2627
import org.springframework.context.support.GenericApplicationContext;
2728
import org.springframework.core.Conventions;
2829
import org.springframework.test.context.TestContext;
@@ -153,10 +154,14 @@ private void injectDependenciesInAotMode(TestContext testContext) throws Excepti
153154

154155
Object bean = testContext.getTestInstance();
155156
Class<?> clazz = testContext.getTestClass();
157+
156158
ConfigurableListableBeanFactory beanFactory = gac.getBeanFactory();
157-
AutowiredAnnotationBeanPostProcessor beanPostProcessor = new AutowiredAnnotationBeanPostProcessor();
158-
beanPostProcessor.setBeanFactory(beanFactory);
159-
beanPostProcessor.processInjection(bean);
159+
AutowiredAnnotationBeanPostProcessor autowiredAnnotationBpp = new AutowiredAnnotationBeanPostProcessor();
160+
autowiredAnnotationBpp.setBeanFactory(beanFactory);
161+
autowiredAnnotationBpp.processInjection(bean);
162+
CommonAnnotationBeanPostProcessor commonAnnotationBpp = new CommonAnnotationBeanPostProcessor();
163+
commonAnnotationBpp.setBeanFactory(beanFactory);
164+
commonAnnotationBpp.processInjection(bean);
160165
beanFactory.initializeBean(bean, clazz.getName() + AutowireCapableBeanFactory.ORIGINAL_INSTANCE_SUFFIX);
161166
testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
162167
}

spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicSpringJupiterTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

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

19+
import jakarta.annotation.Resource;
1920
import org.junit.jupiter.api.Nested;
2021

2122
import org.springframework.beans.factory.annotation.Autowired;
@@ -57,11 +58,15 @@
5758
})
5859
public class BasicSpringJupiterTests {
5960

61+
@Resource
62+
Integer magicNumber;
63+
6064
@org.junit.jupiter.api.Test
6165
void test(@Autowired ApplicationContext context, @Autowired MessageService messageService,
6266
@Value("${test.engine}") String testEngine) {
6367
assertThat(messageService.generateMessage()).isEqualTo("Hello, AOT!");
6468
assertThat(testEngine).isEqualTo("jupiter");
69+
assertThat(magicNumber).isEqualTo(42);
6570
assertEnvProperties(context);
6671
}
6772

spring-test/src/test/java/org/springframework/test/context/aot/samples/basic/BasicTestConfiguration.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -45,4 +45,9 @@ MessageService spanishMessageService() {
4545
return new SpanishMessageService();
4646
}
4747

48+
@Bean
49+
Number magicNumber() {
50+
return 42;
51+
}
52+
4853
}

0 commit comments

Comments
 (0)