View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertSame;
5   
6   import java.net.InetAddress;
7   import java.net.UnknownHostException;
8   
9   import javax.servlet.Servlet;
10  import javax.servlet.ServletConfig;
11  import javax.servlet.ServletRequest;
12  import javax.servlet.ServletResponse;
13  
14  import org.junit.jupiter.api.AfterEach;
15  import org.junit.jupiter.api.BeforeEach;
16  import org.junit.jupiter.api.Disabled;
17  import org.junit.jupiter.api.Test;
18  
19  /**
20   * The Class TestedClassWithConstructorDI2Test.
21   */
22  // TODO JWL 2/18/2024 Test not allowed on jdk21
23  @Disabled
24  final class TestedClassWithConstructorDI2Test {
25  
26      /**
27       * The Class TestedClass.
28       */
29      public static final class TestedClass implements Servlet {
30  
31          /** The counter. */
32          static int counter;
33  
34          /** The config. */
35          private ServletConfig config;
36  
37          /** The dependency 1. */
38          private final Dependency dependency1;
39  
40          /** The dependency 2. */
41          private final Dependency dependency2;
42  
43          /** The dependency 3. */
44          private final Dependency dependency3;
45  
46          /**
47           * Instantiates a new tested class.
48           *
49           * @param dependency1
50           *            the dependency 1
51           * @param r
52           *            the r
53           * @param dependency2
54           *            the dependency 2
55           * @param dependency3
56           *            the dependency 3
57           */
58          public TestedClass(Dependency dependency1, Runnable r, Dependency dependency2, Dependency dependency3) {
59              this.dependency1 = dependency1;
60              this.dependency2 = dependency2;
61              this.dependency3 = dependency3;
62              r.run();
63  
64              int i = dependency1.doSomething();
65              assert i == 123;
66  
67              try {
68                  InetAddress localHost = InetAddress.getLocalHost();
69                  assert localHost.getHostName() == null;
70              } catch (UnknownHostException e) {
71                  throw new IllegalStateException("InetAddress should be mocked", e);
72              }
73          }
74  
75          /**
76           * Do some operation.
77           *
78           * @return the int
79           */
80          public int doSomeOperation() {
81              return dependency1.doSomething() + dependency2.doSomething();
82          }
83  
84          @Override
85          public ServletConfig getServletConfig() {
86              return config;
87          }
88  
89          @Override
90          public void service(ServletRequest req, ServletResponse res) {
91          }
92  
93          @Override
94          public String getServletInfo() {
95              return null;
96          }
97  
98          @Override
99          public void init(ServletConfig cfg) {
100             config = cfg;
101             counter++;
102 
103             int i = dependency1.doSomething();
104             assert i == 123;
105 
106             checkInetAddressMocking();
107         }
108 
109         /**
110          * Check inet address mocking.
111          */
112         private void checkInetAddressMocking() {
113             try {
114                 InetAddress inetAddress = InetAddress.getByName("testHost");
115                 assert inetAddress.getHostName() == null;
116             } catch (UnknownHostException ignore) {
117                 counter = -1;
118             }
119         }
120 
121         @Override
122         public void destroy() {
123             counter++;
124             checkInetAddressMocking();
125         }
126     }
127 
128     /**
129      * The Class Dependency.
130      */
131     static class Dependency {
132         /**
133          * Do something.
134          *
135          * @return the int
136          */
137         int doSomething() {
138             return -1;
139         }
140     }
141 
142     /** The tested. */
143     @Tested
144     TestedClass tested;
145 
146     /** The task. */
147     @Injectable
148     Runnable task;
149 
150     /** The dependency 1. */
151     @Injectable
152     Dependency dependency1;
153 
154     /** The dependency 2. */
155     @Injectable
156     Dependency dependency2;
157 
158     /** The config. */
159     @Injectable
160     ServletConfig config;
161 
162     /** The test host. */
163     @Mocked
164     InetAddress testHost;
165 
166     /**
167      * Reset counter.
168      */
169     @BeforeEach
170     void resetCounter() {
171         TestedClass.counter = 0;
172         new Expectations() {
173             {
174                 dependency1.doSomething();
175                 result = 123;
176             }
177         };
178     }
179 
180     /**
181      * Exercise tested object with dependencies of same type injected through constructor.
182      *
183      * @param dependency3
184      *            the dependency 3
185      */
186     @Test
187     void exerciseTestedObjectWithDependenciesOfSameTypeInjectedThroughConstructor(@Injectable Dependency dependency3) {
188         assertTestedObjectWasInitialized();
189         assertSame(dependency3, tested.dependency3);
190 
191         new Expectations() {
192             {
193                 dependency1.doSomething();
194                 result = 23;
195                 dependency2.doSomething();
196                 result = 5;
197             }
198         };
199 
200         assertEquals(28, tested.doSomeOperation());
201     }
202 
203     /**
204      * Exercise tested object with extra injectable parameter.
205      *
206      * @param dependency3
207      *            the dependency 3
208      * @param mock4
209      *            the mock 4
210      */
211     @Test
212     void exerciseTestedObjectWithExtraInjectableParameter(@Injectable Dependency dependency3,
213             @Injectable Dependency mock4) {
214         assertTestedObjectWasInitialized();
215         assertSame(dependency1, tested.dependency1);
216         assertSame(dependency2, tested.dependency2);
217         assertSame(dependency3, tested.dependency3);
218     }
219 
220     /**
221      * Assert tested object was initialized.
222      */
223     void assertTestedObjectWasInitialized() {
224         assertSame(config, tested.getServletConfig());
225         assertEquals(1, TestedClass.counter);
226     }
227 
228     /**
229      * Verify tested object after every test.
230      */
231     @AfterEach
232     void verifyTestedObjectAfterEveryTest() {
233         assertEquals(2, TestedClass.counter);
234     }
235 }