Skip to content

Commit a3b979c

Browse files
committed
Register runtime hints for @⁠TestBean fully-qualified method names
This commit introduces a TestBeanReflectiveProcessor that registers GraalVM native image reflection hints for a fully-qualified method name configured via @⁠TestBean. Closes gh-33836
1 parent a8f5848 commit a3b979c

File tree

5 files changed

+174
-14
lines changed

5 files changed

+174
-14
lines changed

spring-test/src/main/java/org/springframework/test/context/bean/override/convention/TestBean.java

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
2424

25+
import org.springframework.aot.hint.annotation.Reflective;
2526
import org.springframework.core.annotation.AliasFor;
2627
import org.springframework.test.context.bean.override.BeanOverride;
2728

@@ -115,6 +116,7 @@
115116
@Retention(RetentionPolicy.RUNTIME)
116117
@Documented
117118
@BeanOverride(TestBeanOverrideProcessor.class)
119+
@Reflective(TestBeanReflectiveProcessor.class)
118120
public @interface TestBean {
119121

120122
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2002-2024 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.test.context.bean.override.convention;
18+
19+
import java.lang.reflect.AnnotatedElement;
20+
import java.util.List;
21+
22+
import org.springframework.aot.hint.ReflectionHints;
23+
import org.springframework.aot.hint.TypeReference;
24+
import org.springframework.aot.hint.annotation.ReflectiveProcessor;
25+
import org.springframework.core.annotation.MergedAnnotation;
26+
import org.springframework.core.annotation.MergedAnnotations;
27+
import org.springframework.util.Assert;
28+
29+
import static org.springframework.aot.hint.ExecutableMode.INVOKE;
30+
31+
/**
32+
* {@link ReflectiveProcessor} that processes {@link TestBean @TestBean} annotations.
33+
*
34+
* @author Sam Brannen
35+
* @since 6.2
36+
*/
37+
class TestBeanReflectiveProcessor implements ReflectiveProcessor {
38+
39+
@Override
40+
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
41+
MergedAnnotations.from(element)
42+
.get(TestBean.class)
43+
.synthesize(MergedAnnotation::isPresent)
44+
.map(TestBean::methodName)
45+
.filter(methodName -> methodName.contains("#"))
46+
.ifPresent(methodName -> {
47+
int indexOfHash = methodName.lastIndexOf('#');
48+
String className = methodName.substring(0, indexOfHash).trim();
49+
Assert.hasText(className, () -> "No class name present in fully-qualified method name: " + methodName);
50+
String methodNameToUse = methodName.substring(indexOfHash + 1).trim();
51+
Assert.hasText(methodNameToUse, () -> "No method name present in fully-qualified method name: " + methodName);
52+
hints.registerType(TypeReference.of(className), builder ->
53+
builder.withMethod(methodNameToUse, List.of(), INVOKE));
54+
});
55+
}
56+
57+
}

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

+41-14
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
5454
import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests;
5555
import org.springframework.test.context.aot.samples.bean.override.EasyMockBeanJupiterTests;
56+
import org.springframework.test.context.aot.samples.bean.override.GreetingServiceFactory;
5657
import org.springframework.test.context.aot.samples.bean.override.MockitoBeanJupiterTests;
58+
import org.springframework.test.context.aot.samples.bean.override.TestBeanJupiterTests;
5759
import org.springframework.test.context.aot.samples.common.GreetingService;
5860
import org.springframework.test.context.aot.samples.common.MessageService;
5961
import org.springframework.test.context.aot.samples.jdbc.SqlScriptsSpringJupiterTests;
@@ -108,6 +110,7 @@ void endToEndTests() {
108110
BasicSpringVintageTests.class,
109111
EasyMockBeanJupiterTests.class,
110112
MockitoBeanJupiterTests.class,
113+
TestBeanJupiterTests.class,
111114
SqlScriptsSpringJupiterTests.class,
112115
XmlSpringJupiterTests.class,
113116
WebSpringJupiterTests.class);
@@ -162,6 +165,9 @@ void endToEndTests() {
162165
else if (testClass.getPackageName().contains("jdbc")) {
163166
assertContextForJdbcTests(context);
164167
}
168+
else if (testClass.equals(TestBeanJupiterTests.class)) {
169+
assertContextForTestBeanOverrideTests(context);
170+
}
165171
else if (testClass.equals(EasyMockBeanJupiterTests.class)) {
166172
assertContextForEasyMockBeanOverrideTests(context);
167173
}
@@ -275,12 +281,18 @@ private static void assertRuntimeHints(RuntimeHints runtimeHints) {
275281

276282
// @BeanOverride(value = ...)
277283
Stream.of(
284+
// @TestBean
285+
"org.springframework.test.context.bean.override.convention.TestBeanOverrideProcessor",
278286
// @MockitoBean
279287
"org.springframework.test.context.bean.override.mockito.MockitoBeanOverrideProcessor",
280288
// @EasyMockBean
281289
"org.springframework.test.context.bean.override.easymock.EasyMockBeanOverrideProcessor"
282290
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));
283291

292+
// @TestBean(methodName = <fully-qualified method name>)
293+
assertThat(reflection().onMethod(GreetingServiceFactory.class, "createEnigmaGreetingService"))
294+
.accepts(runtimeHints);
295+
284296
// GenericApplicationContext.preDetermineBeanTypes() should have registered proxy
285297
// hints for the EasyMock interface-based mocks.
286298
assertProxyRegistered(runtimeHints, GreetingService.class);
@@ -346,6 +358,11 @@ private void assertContextForJdbcTests(ApplicationContext context) {
346358
assertThat(context.getBean(DataSource.class)).as("DataSource").isNotNull();
347359
}
348360

361+
private void assertContextForTestBeanOverrideTests(ApplicationContext context) {
362+
GreetingService greetingService = context.getBean(GreetingService.class);
363+
assertThat(greetingService.greeting()).isEqualTo("enigma");
364+
}
365+
349366
private void assertContextForEasyMockBeanOverrideTests(ApplicationContext context) {
350367
GreetingService greetingService = context.getBean(GreetingService.class);
351368
MessageService messageService = context.getBean(MessageService.class);
@@ -512,29 +529,39 @@ record Mapping(MergedContextConfiguration mergedConfig, ClassName className) {
512529
"org/springframework/test/context/aot/samples/bean/override/MockitoBeanJupiterTests__TestContext006_BeanDefinitions.java",
513530
"org/springframework/test/context/aot/samples/bean/override/MockitoBeanJupiterTests__TestContext006_BeanFactoryRegistrations.java",
514531
"org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext006_BeanDefinitions.java",
515-
// SqlScriptsSpringJupiterTests
532+
533+
// TestBeanJupiterTests
516534
"org/springframework/context/event/DefaultEventListenerFactory__TestContext007_BeanDefinitions.java",
517535
"org/springframework/context/event/EventListenerMethodProcessor__TestContext007_BeanDefinitions.java",
518-
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext007_ApplicationContextInitializer.java",
519-
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext007_BeanFactoryRegistrations.java",
520-
"org/springframework/test/context/jdbc/EmptyDatabaseConfig__TestContext007_BeanDefinitions.java",
536+
"org/springframework/test/context/aot/samples/bean/override/TestBeanJupiterTests__TestContext007_ApplicationContextInitializer.java",
537+
"org/springframework/test/context/aot/samples/bean/override/TestBeanJupiterTests__TestContext007_BeanFactoryRegistrations.java",
521538
"org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext007_BeanDefinitions.java",
522-
// WebSpringJupiterTests
539+
540+
// SqlScriptsSpringJupiterTests
523541
"org/springframework/context/event/DefaultEventListenerFactory__TestContext008_BeanDefinitions.java",
524542
"org/springframework/context/event/EventListenerMethodProcessor__TestContext008_BeanDefinitions.java",
525-
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext008_ApplicationContextInitializer.java",
526-
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext008_BeanFactoryRegistrations.java",
527-
"org/springframework/test/context/aot/samples/web/WebTestConfiguration__TestContext008_BeanDefinitions.java",
543+
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext008_ApplicationContextInitializer.java",
544+
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext008_BeanFactoryRegistrations.java",
545+
"org/springframework/test/context/jdbc/EmptyDatabaseConfig__TestContext008_BeanDefinitions.java",
528546
"org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext008_BeanDefinitions.java",
529-
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext008_Autowiring.java",
530-
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext008_BeanDefinitions.java",
531-
// XmlSpringJupiterTests
547+
548+
// WebSpringJupiterTests
532549
"org/springframework/context/event/DefaultEventListenerFactory__TestContext009_BeanDefinitions.java",
533550
"org/springframework/context/event/EventListenerMethodProcessor__TestContext009_BeanDefinitions.java",
534-
"org/springframework/test/context/aot/samples/common/DefaultMessageService__TestContext009_BeanDefinitions.java",
535-
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext009_ApplicationContextInitializer.java",
536-
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext009_BeanFactoryRegistrations.java",
551+
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext009_ApplicationContextInitializer.java",
552+
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext009_BeanFactoryRegistrations.java",
553+
"org/springframework/test/context/aot/samples/web/WebTestConfiguration__TestContext009_BeanDefinitions.java",
537554
"org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext009_BeanDefinitions.java",
555+
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext009_Autowiring.java",
556+
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext009_BeanDefinitions.java",
557+
558+
// XmlSpringJupiterTests
559+
"org/springframework/context/event/DefaultEventListenerFactory__TestContext010_BeanDefinitions.java",
560+
"org/springframework/context/event/EventListenerMethodProcessor__TestContext010_BeanDefinitions.java",
561+
"org/springframework/test/context/aot/samples/common/DefaultMessageService__TestContext010_BeanDefinitions.java",
562+
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext010_ApplicationContextInitializer.java",
563+
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext010_BeanFactoryRegistrations.java",
564+
"org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext010_BeanDefinitions.java"
538565
};
539566

540567
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2002-2024 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.test.context.aot.samples.bean.override;
18+
19+
import org.springframework.test.context.aot.samples.common.GreetingService;
20+
21+
/**
22+
* @author Sam Brannen
23+
* @since 6.2
24+
*/
25+
public class GreetingServiceFactory {
26+
27+
public static GreetingService createEnigmaGreetingService() {
28+
return () -> "enigma";
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2002-2024 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.test.context.aot.samples.bean.override;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
22+
import org.springframework.test.context.aot.samples.common.GreetingService;
23+
import org.springframework.test.context.bean.override.convention.TestBean;
24+
import org.springframework.test.context.junit.jupiter.SpringExtension;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* @author Sam Brannen
30+
* @since 6.2
31+
*/
32+
@ExtendWith(SpringExtension.class)
33+
public class TestBeanJupiterTests {
34+
35+
@TestBean(methodName = "org.springframework.test.context.aot.samples.bean.override.GreetingServiceFactory#createEnigmaGreetingService")
36+
GreetingService greetingService;
37+
38+
@Test
39+
void test() {
40+
assertThat(greetingService.greeting()).isEqualTo("enigma");
41+
}
42+
43+
}

0 commit comments

Comments
 (0)