View Javadoc
1   /*
2    * MIT License
3    * Copyright (c) 2006-2025 JMockit developers
4    * See LICENSE file for full license text.
5    */
6   package otherTests.testng;
7   
8   import static org.testng.Assert.assertEquals;
9   import static org.testng.Assert.assertNotNull;
10  import static org.testng.Assert.assertNotSame;
11  import static org.testng.Assert.assertNull;
12  import static org.testng.Assert.assertSame;
13  
14  import java.util.concurrent.Callable;
15  
16  import mockit.Expectations;
17  import mockit.Injectable;
18  import mockit.Tested;
19  import mockit.Verifications;
20  
21  import org.testng.annotations.AfterMethod;
22  import org.testng.annotations.BeforeMethod;
23  import org.testng.annotations.Test;
24  
25  public final class TestedAndInjectablesTest {
26      static final class UtilityClass {
27          String name;
28          Collaborator collaborator1;
29          Collaborator collaborator2;
30      }
31  
32      static class Collaborator {
33          void doSomething() {
34          }
35      }
36  
37      static class SUT {
38          final Collaborator collaborator1;
39          Collaborator collaborator2;
40  
41          SUT(Collaborator collaborator1) {
42              this.collaborator1 = collaborator1;
43          }
44  
45          void useCollaborators() {
46              collaborator1.doSomething();
47              collaborator2.doSomething();
48          }
49      }
50  
51      @Tested(availableDuringSetup = true)
52      UtilityClass util;
53      UtilityClass previousUtilityClassInstance;
54      @Injectable("util")
55      String utilName;
56  
57      @Tested
58      SUT tested1;
59      @Injectable
60      Collaborator collaborator1;
61  
62      @Tested
63      SUT tested2;
64      @Tested
65      final SUT tested3 = new SUT(new Collaborator());
66      @Tested
67      final SUT tested4 = null;
68  
69      SUT firstTestedObject;
70      Collaborator firstMockedObject;
71  
72      @BeforeMethod
73      public void setUp() {
74          assertUtilObjectIsAvailable();
75          tested2 = new SUT(new Collaborator());
76      }
77  
78      void assertUtilObjectIsAvailable() {
79          assertNotNull(util);
80          assertEquals(util.name, "util");
81          assertSame(collaborator1, util.collaborator1);
82      }
83  
84      @AfterMethod
85      public void tearDown() {
86          assertUtilObjectIsAvailable();
87      }
88  
89      @Injectable
90      Collaborator collaborator2;
91  
92      @Test
93      public void firstTest() {
94          assertSame(collaborator1, util.collaborator1);
95          assertSame(collaborator2, util.collaborator2);
96  
97          assertNotNull(tested1);
98          firstTestedObject = tested1;
99  
100         assertNotNull(collaborator1);
101         firstMockedObject = collaborator1;
102 
103         assertNotNull(collaborator2);
104 
105         assertStatesOfTestedObjects(collaborator2);
106 
107         new Expectations() {
108             {
109                 collaborator1.doSomething();
110                 collaborator2.doSomething();
111             }
112         };
113 
114         tested1.useCollaborators();
115 
116         previousUtilityClassInstance = util;
117     }
118 
119     void assertStatesOfTestedObjects(Collaborator col2) {
120         assertSame(tested1.collaborator1, collaborator1);
121         assertSame(tested1.collaborator2, col2);
122 
123         assertNotSame(tested2.collaborator1, collaborator1);
124         assertSame(tested2.collaborator2, col2);
125 
126         assertNotSame(tested3.collaborator1, collaborator1);
127         assertNotNull(tested3.collaborator2);
128 
129         assertNull(tested4);
130     }
131 
132     @Test(dependsOnMethods = "firstTest")
133     public void secondTest() {
134         assertSame(collaborator1, util.collaborator1);
135 
136         assertSame(collaborator1, firstMockedObject);
137         assertNotSame(tested1, firstTestedObject);
138 
139         assertStatesOfTestedObjects(collaborator2);
140 
141         assertNotSame(util, previousUtilityClassInstance);
142     }
143 
144     @Injectable
145     Callable<String> mock;
146 
147     @Test
148     public void recordAndVerifyExpectationsOnMockedInterface() throws Exception {
149         new Expectations() {
150             {
151                 mock.call();
152                 result = "test";
153                 minTimes = 0;
154             }
155         };
156 
157         String value = mock.call();
158 
159         assertEquals(value, "test");
160         new Verifications() {
161             {
162                 mock.call();
163                 times = 1;
164             }
165         };
166     }
167 }