16
16
package org .springframework .data .aot ;
17
17
18
18
import static org .assertj .core .api .Assertions .*;
19
+ import static org .mockito .ArgumentMatchers .*;
20
+ import static org .mockito .Mockito .*;
19
21
20
22
import java .util .Collections ;
21
23
import java .util .function .Consumer ;
28
30
import org .springframework .aot .generate .InMemoryGeneratedFiles ;
29
31
import org .springframework .aot .hint .RuntimeHints ;
30
32
import org .springframework .aot .hint .predicate .RuntimeHintsPredicates ;
33
+ import org .springframework .beans .factory .BeanCreationException ;
31
34
import org .springframework .beans .factory .aot .BeanRegistrationAotContribution ;
32
35
import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
33
36
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
@@ -51,7 +54,7 @@ class ManagedTypesBeanRegistrationAotProcessorUnitTests {
51
54
52
55
@ BeforeEach
53
56
void beforeEach () {
54
- beanFactory = new DefaultListableBeanFactory ();
57
+ beanFactory = spy ( new DefaultListableBeanFactory () );
55
58
}
56
59
57
60
@ Test // GH-2593
@@ -65,6 +68,16 @@ void processesBeanWithMatchingModulePrefix() {
65
68
assertThat (contribution ).isNotNull ();
66
69
}
67
70
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
+
68
81
@ Test // GH-2593
69
82
void contributesReflectionForManagedTypes () {
70
83
@@ -94,6 +107,16 @@ void processesMatchingSubtypeBean() {
94
107
assertThat (contribution ).isNotNull ();
95
108
}
96
109
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
+
97
120
@ Test // GH-2593
98
121
void ignoresBeanNotMatchingRequiredType () {
99
122
@@ -117,6 +140,26 @@ void ignoresBeanNotMatchingPrefix() {
117
140
assertThat (contribution ).isNull ();
118
141
}
119
142
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
+
120
163
private ManagedTypesBeanRegistrationAotProcessor createPostProcessor (String moduleIdentifier ) {
121
164
ManagedTypesBeanRegistrationAotProcessor postProcessor = new ManagedTypesBeanRegistrationAotProcessor ();
122
165
postProcessor .setModuleIdentifier (moduleIdentifier );
0 commit comments