View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package mockit;
7   
8   import static org.junit.jupiter.api.Assertions.assertEquals;
9   
10  import mockit.integration.junit5.JMockitExtension;
11  
12  import org.junit.jupiter.api.Test;
13  import org.junit.jupiter.api.extension.ExtendWith;
14  
15  /**
16   * The Class TestedClassWithFieldDIByTypeAndNameTest.
17   */
18  @ExtendWith(JMockitExtension.class)
19  class TestedClassWithFieldDIByTypeAndNameTest {
20  
21      /**
22       * The Class TestedClass.
23       */
24      static class TestedClass {
25  
26          /** The some value. */
27          int someValue;
28  
29          /** The another value. */
30          int anotherValue;
31  
32          /**
33           * Gets the some value base.
34           *
35           * @return the some value base
36           */
37          final int getSomeValue_base() {
38              return someValue;
39          }
40      }
41  
42      /**
43       * The Class TestedSubclass.
44       */
45      static class TestedSubclass extends TestedClass {
46  
47          /** The some value. */
48          @SuppressWarnings("FieldNameHidesFieldInSuperclass")
49          int someValue;
50  
51          /** The yet another value. */
52          int yetAnotherValue;
53      }
54  
55      /** The tested. */
56      @Tested
57      TestedSubclass tested;
58  
59      /**
60       * Inject by field type and name with tested class having multiple fields of same type.
61       *
62       * @param anotherValue
63       *            the another value
64       */
65      @Test
66      void injectByFieldTypeAndNameWithTestedClassHavingMultipleFieldsOfSameType(@Injectable("12") int anotherValue) {
67          assertEquals(0, tested.getSomeValue_base());
68          assertEquals(0, tested.someValue);
69          assertEquals(12, tested.anotherValue);
70          assertEquals(0, tested.yetAnotherValue);
71      }
72  
73      /**
74       * Inject by field type and name with tested class having fields of same type but different names.
75       *
76       * @param val
77       *            the val
78       */
79      @Test
80      void injectByFieldTypeAndNameWithTestedClassHavingFieldsOfSameTypeButDifferentNames(@Injectable("45") int val) {
81          assertEquals(0, tested.getSomeValue_base());
82          assertEquals(0, tested.someValue);
83          assertEquals(0, tested.anotherValue);
84          assertEquals(0, tested.yetAnotherValue);
85      }
86  
87      /**
88       * Inject by field type and name into fields at different levels of class hierarchy.
89       *
90       * @param someValue
91       *            the some value
92       * @param yetAnotherValue
93       *            the yet another value
94       * @param unused
95       *            the unused
96       */
97      @Test
98      void injectByFieldTypeAndNameIntoFieldsAtDifferentLevelsOfClassHierarchy(@Injectable("1") int someValue,
99              @Injectable("2") int yetAnotherValue, @Injectable("3") int unused) {
100         assertEquals(0, tested.getSomeValue_base());
101         assertEquals(1, tested.someValue);
102         assertEquals(0, tested.anotherValue);
103         assertEquals(2, tested.yetAnotherValue);
104     }
105 }