@@ -120,6 +120,21 @@ public void beforeEach(ExtensionContext context) throws Exception
120
120
}
121
121
}
122
122
123
+ private String getFieldNamed (Field field )
124
+ {
125
+ jakarta .inject .Named jakartaAnnotation = field .getAnnotation (jakarta .inject .Named .class );
126
+ if (jakartaAnnotation != null ) {
127
+ return jakartaAnnotation .value ();
128
+ }
129
+
130
+ Named javaxAnnotation = field .getAnnotation (Named .class );
131
+ if (javaxAnnotation != null ) {
132
+ return javaxAnnotation .value ();
133
+ }
134
+
135
+ return null ;
136
+ }
137
+
123
138
private void initializeTestInstance (Object testInstance , ExtensionContext context ) throws Exception
124
139
{
125
140
// Make sure tests don't leak one on another
@@ -164,13 +179,13 @@ private void initializeTestInstance(Object testInstance, ExtensionContext contex
164
179
MockComponent mockComponentAnnotation = field .getAnnotation (MockComponent .class );
165
180
if (mockComponentAnnotation != null ) {
166
181
// Get the hint from the @Named annotation (if any)
167
- Named namedAnnotation = field . getAnnotation ( Named . class );
182
+ String named = getFieldNamed ( field );
168
183
Class <?> classToMock = mockComponentAnnotation .classToMock () != MockComponent .class
169
184
? mockComponentAnnotation .classToMock () : null ;
170
185
Object mockComponent ;
171
- if (namedAnnotation != null ) {
186
+ if (named != null ) {
172
187
mockComponent =
173
- mcm .registerMockComponent (field .getGenericType (), namedAnnotation . value () , classToMock , true );
188
+ mcm .registerMockComponent (field .getGenericType (), named , classToMock , true );
174
189
} else {
175
190
mockComponent = mcm .registerMockComponent (field .getGenericType (), null , classToMock , true );
176
191
}
@@ -226,16 +241,14 @@ protected void processSingleInjectMockComponents(Object testInstance, Field fiel
226
241
private void processInjectAnnotations (Object testInstance , MockitoComponentManager mcm ) throws Exception
227
242
{
228
243
for (Field field : ReflectionUtils .getAllFields (testInstance .getClass ())) {
229
- Inject injectAnnotation = field .getAnnotation (Inject .class );
230
- if (injectAnnotation != null ) {
231
- Named namedAnnotation = field .getAnnotation (Named .class );
232
- processSingleInjectAnnotations (testInstance , field , namedAnnotation , mcm );
244
+ if (field .getAnnotation (Inject .class ) != null || field .getAnnotation (jakarta .inject .Inject .class ) != null ) {
245
+ processSingleInjectAnnotations (testInstance , field , getFieldNamed (field ), mcm );
233
246
}
234
247
}
235
248
}
236
249
237
250
private void processSingleInjectAnnotations (Object testInstance , Field field ,
238
- Named namedAnnotation , MockitoComponentManager mcm ) throws Exception
251
+ String namedAnnotation , MockitoComponentManager mcm ) throws Exception
239
252
{
240
253
// Must be an instance
241
254
if (!field .getType ().isInterface ()) {
@@ -244,7 +257,7 @@ private void processSingleInjectAnnotations(Object testInstance, Field field,
244
257
}
245
258
246
259
Object component = (namedAnnotation == null ) ? mcm .getInstance (field .getGenericType ())
247
- : mcm .getInstance (field .getGenericType (), namedAnnotation . value () );
260
+ : mcm .getInstance (field .getGenericType (), namedAnnotation );
248
261
ReflectionUtils .setFieldValue (testInstance , field .getName (), component );
249
262
}
250
263
0 commit comments