Skip to content

Commit

Permalink
Register runtime hints for @⁠TestBean fully-qualified method names
Browse files Browse the repository at this point in the history
This commit introduces a TestBeanReflectiveProcessor that registers
GraalVM native image reflection hints for a fully-qualified method name
configured via @⁠TestBean.

Closes gh-33836
  • Loading branch information
sbrannen committed Nov 1, 2024
1 parent a8f5848 commit a3b979c
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.aot.hint.annotation.Reflective;
import org.springframework.core.annotation.AliasFor;
import org.springframework.test.context.bean.override.BeanOverride;

Expand Down Expand Up @@ -115,6 +116,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Documented
@BeanOverride(TestBeanOverrideProcessor.class)
@Reflective(TestBeanReflectiveProcessor.class)
public @interface TestBean {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.bean.override.convention;

import java.lang.reflect.AnnotatedElement;
import java.util.List;

import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.annotation.ReflectiveProcessor;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.util.Assert;

import static org.springframework.aot.hint.ExecutableMode.INVOKE;

/**
* {@link ReflectiveProcessor} that processes {@link TestBean @TestBean} annotations.
*
* @author Sam Brannen
* @since 6.2
*/
class TestBeanReflectiveProcessor implements ReflectiveProcessor {

@Override
public void registerReflectionHints(ReflectionHints hints, AnnotatedElement element) {
MergedAnnotations.from(element)
.get(TestBean.class)
.synthesize(MergedAnnotation::isPresent)
.map(TestBean::methodName)
.filter(methodName -> methodName.contains("#"))
.ifPresent(methodName -> {
int indexOfHash = methodName.lastIndexOf('#');
String className = methodName.substring(0, indexOfHash).trim();
Assert.hasText(className, () -> "No class name present in fully-qualified method name: " + methodName);
String methodNameToUse = methodName.substring(indexOfHash + 1).trim();
Assert.hasText(methodNameToUse, () -> "No method name present in fully-qualified method name: " + methodName);
hints.registerType(TypeReference.of(className), builder ->
builder.withMethod(methodNameToUse, List.of(), INVOKE));
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
import org.springframework.test.context.aot.samples.basic.BasicSpringTestNGTests;
import org.springframework.test.context.aot.samples.basic.BasicSpringVintageTests;
import org.springframework.test.context.aot.samples.bean.override.EasyMockBeanJupiterTests;
import org.springframework.test.context.aot.samples.bean.override.GreetingServiceFactory;
import org.springframework.test.context.aot.samples.bean.override.MockitoBeanJupiterTests;
import org.springframework.test.context.aot.samples.bean.override.TestBeanJupiterTests;
import org.springframework.test.context.aot.samples.common.GreetingService;
import org.springframework.test.context.aot.samples.common.MessageService;
import org.springframework.test.context.aot.samples.jdbc.SqlScriptsSpringJupiterTests;
Expand Down Expand Up @@ -108,6 +110,7 @@ void endToEndTests() {
BasicSpringVintageTests.class,
EasyMockBeanJupiterTests.class,
MockitoBeanJupiterTests.class,
TestBeanJupiterTests.class,
SqlScriptsSpringJupiterTests.class,
XmlSpringJupiterTests.class,
WebSpringJupiterTests.class);
Expand Down Expand Up @@ -162,6 +165,9 @@ void endToEndTests() {
else if (testClass.getPackageName().contains("jdbc")) {
assertContextForJdbcTests(context);
}
else if (testClass.equals(TestBeanJupiterTests.class)) {
assertContextForTestBeanOverrideTests(context);
}
else if (testClass.equals(EasyMockBeanJupiterTests.class)) {
assertContextForEasyMockBeanOverrideTests(context);
}
Expand Down Expand Up @@ -275,12 +281,18 @@ private static void assertRuntimeHints(RuntimeHints runtimeHints) {

// @BeanOverride(value = ...)
Stream.of(
// @TestBean
"org.springframework.test.context.bean.override.convention.TestBeanOverrideProcessor",
// @MockitoBean
"org.springframework.test.context.bean.override.mockito.MockitoBeanOverrideProcessor",
// @EasyMockBean
"org.springframework.test.context.bean.override.easymock.EasyMockBeanOverrideProcessor"
).forEach(type -> assertReflectionRegistered(runtimeHints, type, INVOKE_DECLARED_CONSTRUCTORS));

// @TestBean(methodName = <fully-qualified method name>)
assertThat(reflection().onMethod(GreetingServiceFactory.class, "createEnigmaGreetingService"))
.accepts(runtimeHints);

// GenericApplicationContext.preDetermineBeanTypes() should have registered proxy
// hints for the EasyMock interface-based mocks.
assertProxyRegistered(runtimeHints, GreetingService.class);
Expand Down Expand Up @@ -346,6 +358,11 @@ private void assertContextForJdbcTests(ApplicationContext context) {
assertThat(context.getBean(DataSource.class)).as("DataSource").isNotNull();
}

private void assertContextForTestBeanOverrideTests(ApplicationContext context) {
GreetingService greetingService = context.getBean(GreetingService.class);
assertThat(greetingService.greeting()).isEqualTo("enigma");
}

private void assertContextForEasyMockBeanOverrideTests(ApplicationContext context) {
GreetingService greetingService = context.getBean(GreetingService.class);
MessageService messageService = context.getBean(MessageService.class);
Expand Down Expand Up @@ -512,29 +529,39 @@ record Mapping(MergedContextConfiguration mergedConfig, ClassName className) {
"org/springframework/test/context/aot/samples/bean/override/MockitoBeanJupiterTests__TestContext006_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/bean/override/MockitoBeanJupiterTests__TestContext006_BeanFactoryRegistrations.java",
"org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext006_BeanDefinitions.java",
// SqlScriptsSpringJupiterTests

// TestBeanJupiterTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext007_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext007_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext007_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext007_BeanFactoryRegistrations.java",
"org/springframework/test/context/jdbc/EmptyDatabaseConfig__TestContext007_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/bean/override/TestBeanJupiterTests__TestContext007_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/bean/override/TestBeanJupiterTests__TestContext007_BeanFactoryRegistrations.java",
"org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext007_BeanDefinitions.java",
// WebSpringJupiterTests

// SqlScriptsSpringJupiterTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext008_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext008_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext008_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext008_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/web/WebTestConfiguration__TestContext008_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext008_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/jdbc/SqlScriptsSpringJupiterTests__TestContext008_BeanFactoryRegistrations.java",
"org/springframework/test/context/jdbc/EmptyDatabaseConfig__TestContext008_BeanDefinitions.java",
"org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext008_BeanDefinitions.java",
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext008_Autowiring.java",
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext008_BeanDefinitions.java",
// XmlSpringJupiterTests

// WebSpringJupiterTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext009_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext009_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/common/DefaultMessageService__TestContext009_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext009_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext009_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext009_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/web/WebSpringJupiterTests__TestContext009_BeanFactoryRegistrations.java",
"org/springframework/test/context/aot/samples/web/WebTestConfiguration__TestContext009_BeanDefinitions.java",
"org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext009_BeanDefinitions.java",
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext009_Autowiring.java",
"org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration__TestContext009_BeanDefinitions.java",

// XmlSpringJupiterTests
"org/springframework/context/event/DefaultEventListenerFactory__TestContext010_BeanDefinitions.java",
"org/springframework/context/event/EventListenerMethodProcessor__TestContext010_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/common/DefaultMessageService__TestContext010_BeanDefinitions.java",
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext010_ApplicationContextInitializer.java",
"org/springframework/test/context/aot/samples/xml/XmlSpringJupiterTests__TestContext010_BeanFactoryRegistrations.java",
"org/springframework/test/context/support/DynamicPropertyRegistrarBeanInitializer__TestContext010_BeanDefinitions.java"
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.aot.samples.bean.override;

import org.springframework.test.context.aot.samples.common.GreetingService;

/**
* @author Sam Brannen
* @since 6.2
*/
public class GreetingServiceFactory {

public static GreetingService createEnigmaGreetingService() {
return () -> "enigma";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.aot.samples.bean.override;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import org.springframework.test.context.aot.samples.common.GreetingService;
import org.springframework.test.context.bean.override.convention.TestBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Sam Brannen
* @since 6.2
*/
@ExtendWith(SpringExtension.class)
public class TestBeanJupiterTests {

@TestBean(methodName = "org.springframework.test.context.aot.samples.bean.override.GreetingServiceFactory#createEnigmaGreetingService")
GreetingService greetingService;

@Test
void test() {
assertThat(greetingService.greeting()).isEqualTo("enigma");
}

}

0 comments on commit a3b979c

Please sign in to comment.