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