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   import static org.junit.jupiter.api.Assertions.assertFalse;
10  import static org.junit.jupiter.api.Assertions.assertNotNull;
11  import static org.junit.jupiter.api.Assertions.assertNull;
12  import static org.junit.jupiter.api.Assertions.assertSame;
13  
14  import mockit.integration.junit5.JMockitExtension;
15  
16  import org.junit.jupiter.api.Test;
17  import org.junit.jupiter.api.extension.ExtendWith;
18  
19  class BaseTest {
20      static class Dependency {
21          int doSomething() {
22              return -1;
23          }
24      }
25  
26      static class Collaborator {
27      }
28  
29      public static class TestedClass {
30          protected final int i;
31          protected Dependency dependency;
32          Runnable action;
33  
34          public TestedClass() {
35              i = -1;
36          }
37  
38          public TestedClass(int i) {
39              this.i = i;
40          }
41  
42          public boolean doSomeOperation() {
43              return dependency.doSomething() > 0;
44          }
45      }
46  
47      @Tested
48      TestedClass tested1;
49      @Injectable
50      Dependency dependency;
51      @Tested
52      Collaborator collaborator;
53  
54      final void verifyTestedObjectFromBaseTestClass(int expectedValueForIntField) {
55          assertEquals(expectedValueForIntField, tested1.i);
56          assertSame(dependency, tested1.dependency);
57          assertNotNull(tested1.action);
58      }
59  }
60  
61  /**
62   * The Class TestedClassWithConstructorAndFieldDITest.
63   */
64  @ExtendWith(JMockitExtension.class)
65  class TestedClassWithConstructorAndFieldDITest extends BaseTest {
66  
67      /**
68       * The Class AnotherTestedClass.
69       */
70      @SuppressWarnings("unused")
71      public static class AnotherTestedClass extends TestedClass {
72  
73          /** The another action. */
74          Runnable anotherAction;
75  
76          /** The dependency 3. */
77          Dependency dependency3;
78  
79          /** The dependency 2. */
80          Dependency dependency2;
81  
82          /**
83           * Instantiates a new another tested class.
84           */
85          public AnotherTestedClass() {
86              super(-2);
87          }
88  
89          /**
90           * Instantiates a new another tested class.
91           *
92           * @param value
93           *            the value
94           * @param dependency1
95           *            the dependency 1
96           */
97          public AnotherTestedClass(int value, Dependency dependency1) {
98              super(value);
99              // noinspection UnnecessarySuperQualifier
100             super.dependency = dependency1;
101         }
102 
103         @Override
104         public boolean doSomeOperation() {
105             boolean b = dependency2.doSomething() > 0;
106             return super.doSomeOperation() && b;
107         }
108     }
109 
110     /** The tested 2. */
111     @Tested
112     AnotherTestedClass tested2;
113 
114     /** The another action. */
115     @Injectable
116     Runnable anotherAction;
117 
118     /** The dependency 2. */
119     @Injectable
120     Dependency dependency2;
121 
122     /**
123      * Exercise tested subclass object with fields injected by type and name.
124      */
125     @Test
126     void exerciseTestedSubclassObjectWithFieldsInjectedByTypeAndName() {
127         verifyTestedObjectFromBaseTestClass(-1);
128 
129         assertEquals(-2, tested2.i);
130         assertSame(anotherAction, tested2.anotherAction);
131         assertSame(dependency, tested2.dependency);
132         assertSame(dependency2, tested2.dependency2);
133         assertNull(tested2.dependency3);
134         assertFalse(tested2.doSomeOperation());
135 
136         new Verifications() {
137             {
138                 anotherAction.run();
139                 times = 0;
140                 dependency.doSomething();
141                 times = 1;
142                 dependency2.doSomething();
143             }
144         };
145     }
146 
147     /**
148      * Exercise tested subclass object with fields injected from mock fields and mock parameter.
149      *
150      * @param dependency3
151      *            the dependency 3
152      */
153     @Test
154     void exerciseTestedSubclassObjectWithFieldsInjectedFromMockFieldsAndMockParameter(
155             @Injectable Dependency dependency3) {
156         verifyTestedObjectFromBaseTestClass(-1);
157 
158         assertEquals(-2, tested2.i);
159         assertSame(dependency, tested2.dependency);
160         assertSame(dependency2, tested2.dependency2);
161         assertSame(dependency3, tested2.dependency3);
162         assertSame(anotherAction, tested2.anotherAction);
163         assertFalse(tested2.doSomeOperation());
164     }
165 
166     /**
167      * Exercise tested subclass object using constructor and field injection.
168      *
169      * @param value
170      *            the value
171      * @param dependency1
172      *            the dependency 1
173      */
174     @Test
175     void exerciseTestedSubclassObjectUsingConstructorAndFieldInjection(@Injectable("45") int value,
176             @Injectable Dependency dependency1) {
177         verifyTestedObjectFromBaseTestClass(45);
178 
179         assertEquals(45, tested2.i);
180         assertSame(dependency1, tested2.dependency);
181         assertSame(dependency2, tested2.dependency2);
182         assertNull(tested2.dependency3);
183         assertSame(anotherAction, tested2.anotherAction);
184         assertFalse(tested2.doSomeOperation());
185     }
186 
187     /**
188      * The Class ClassWithFieldHavingTestedFieldInBaseTestClass.
189      */
190     static class ClassWithFieldHavingTestedFieldInBaseTestClass {
191         /** The collaborator. */
192         Collaborator collaborator;
193     }
194 
195     /** The tested 3. */
196     @Tested
197     ClassWithFieldHavingTestedFieldInBaseTestClass tested3;
198 
199     /**
200      * Creates the tested parameter injecting from tested field in base test class.
201      *
202      * @param tested4
203      *            the tested 4
204      */
205     @Test
206     void createTestedParameterInjectingFromTestedFieldInBaseTestClass(
207             @Tested ClassWithFieldHavingTestedFieldInBaseTestClass tested4) {
208         assertNotNull(collaborator);
209         assertSame(collaborator, tested3.collaborator);
210         assertSame(collaborator, tested4.collaborator);
211     }
212 }