1616package org .springframework .data .aot ;
1717
1818import static org .assertj .core .api .Assertions .*;
19+ import static org .mockito .ArgumentMatchers .*;
20+ import static org .mockito .Mockito .*;
1921
2022import java .util .Collections ;
2123import java .util .function .Consumer ;
2830import org .springframework .aot .generate .InMemoryGeneratedFiles ;
2931import org .springframework .aot .hint .RuntimeHints ;
3032import org .springframework .aot .hint .predicate .RuntimeHintsPredicates ;
33+ import org .springframework .beans .factory .BeanCreationException ;
3134import org .springframework .beans .factory .aot .BeanRegistrationAotContribution ;
3235import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
3336import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
@@ -51,7 +54,7 @@ class ManagedTypesBeanRegistrationAotProcessorUnitTests {
5154
5255 @ BeforeEach
5356 void beforeEach () {
54- beanFactory = new DefaultListableBeanFactory ();
57+ beanFactory = spy ( new DefaultListableBeanFactory () );
5558 }
5659
5760 @ Test // GH-2593
@@ -65,6 +68,16 @@ void processesBeanWithMatchingModulePrefix() {
6568 assertThat (contribution ).isNotNull ();
6669 }
6770
71+ @ Test // GH-2593
72+ void processesBeanDefinitionIfPossibleWithoutLoadingTheBean () {
73+
74+ beanFactory .registerBeanDefinition ("commons.managed-types" , managedTypesDefinition );
75+
76+ createPostProcessor ("commons" ).processAheadOfTime (RegisteredBean .of (beanFactory , "commons.managed-types" ));
77+
78+ verify (beanFactory , never ()).getBean (eq ("commons.managed-types" ), eq (ManagedTypes .class ));
79+ }
80+
6881 @ Test // GH-2593
6982 void contributesReflectionForManagedTypes () {
7083
@@ -94,6 +107,16 @@ void processesMatchingSubtypeBean() {
94107 assertThat (contribution ).isNotNull ();
95108 }
96109
110+ @ Test // GH-2593
111+ void processesMatchingSubtypeBeanByAttemptingToLoadItIfNoMatchingConstructorArgumentFound () {
112+
113+ beanFactory .registerBeanDefinition ("commons.managed-types" , myManagedTypesDefinition );
114+
115+ createPostProcessor ("commons" ).processAheadOfTime (RegisteredBean .of (beanFactory , "commons.managed-types" ));
116+
117+ verify (beanFactory ).getBean (eq ("commons.managed-types" ), eq (ManagedTypes .class ));
118+ }
119+
97120 @ Test // GH-2593
98121 void ignoresBeanNotMatchingRequiredType () {
99122
@@ -117,6 +140,26 @@ void ignoresBeanNotMatchingPrefix() {
117140 assertThat (contribution ).isNull ();
118141 }
119142
143+ @ Test // GH-2593
144+ void returnsEmptyContributionWhenBeanCannotBeLoaded () {
145+
146+ doThrow (new BeanCreationException ("o_O" )).when (beanFactory ).getBean (eq ("commons.managed-types" ),
147+ eq (ManagedTypes .class ));
148+
149+ beanFactory .registerBeanDefinition ("commons.managed-types" , myManagedTypesDefinition );
150+
151+ BeanRegistrationAotContribution contribution = createPostProcessor ("commons" )
152+ .processAheadOfTime (RegisteredBean .of (beanFactory , "commons.managed-types" ));
153+
154+ DefaultGenerationContext generationContext = new DefaultGenerationContext (
155+ new GeneratedClasses (new ClassNameGenerator (Object .class )), new InMemoryGeneratedFiles (), new RuntimeHints ());
156+
157+ contribution .applyTo (generationContext , null );
158+
159+ assertThat (generationContext .getRuntimeHints ().reflection ().typeHints ()).isEmpty ();
160+ verify (beanFactory ).getBean (eq ("commons.managed-types" ), eq (ManagedTypes .class ));
161+ }
162+
120163 private ManagedTypesBeanRegistrationAotProcessor createPostProcessor (String moduleIdentifier ) {
121164 ManagedTypesBeanRegistrationAotProcessor postProcessor = new ManagedTypesBeanRegistrationAotProcessor ();
122165 postProcessor .setModuleIdentifier (moduleIdentifier );
0 commit comments