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.assertNotNull;
10  
11  import mockit.integration.junit5.JMockitExtension;
12  
13  import org.junit.jupiter.api.Test;
14  import org.junit.jupiter.api.extension.ExtendWith;
15  
16  /**
17   * The Class TestedClassWithConstructorDI3Test.
18   */
19  @ExtendWith(JMockitExtension.class)
20  class TestedClassWithConstructorDI3Test {
21  
22      /**
23       * The Class TestedClass.
24       */
25      public static final class TestedClass {
26  
27          /** The dependencies. */
28          private final Dependency[] dependencies;
29  
30          /**
31           * Instantiates a new tested class.
32           *
33           * @param r
34           *            the r
35           * @param dependencies
36           *            the dependencies
37           */
38          public TestedClass(Runnable r, Dependency... dependencies) {
39              this.dependencies = dependencies;
40              r.run();
41          }
42  
43          /**
44           * Do some operation.
45           *
46           * @return the int
47           */
48          public int doSomeOperation() {
49              int sum = 0;
50  
51              for (Dependency dependency : dependencies) {
52                  sum += dependency.doSomething();
53              }
54  
55              return sum;
56          }
57      }
58  
59      /**
60       * The Class Dependency.
61       */
62      static class Dependency {
63  
64          /**
65           * Do something.
66           *
67           * @return the int
68           */
69          int doSomething() {
70              return -1;
71          }
72      }
73  
74      /** The support. */
75      @Tested(availableDuringSetup = true)
76      TestedClass support;
77  
78      /** The tested. */
79      @Tested
80      TestedClass tested;
81  
82      /** The mock 1. */
83      @Injectable
84      Dependency mock1;
85  
86      /** The task. */
87      @Injectable
88      Runnable task;
89  
90      /** The mock 2. */
91      @Injectable
92      Dependency mock2;
93  
94      /**
95       * Exercise tested object with dependencies of same type injected through varargs constructor parameter.
96       */
97      @Test
98      void exerciseTestedObjectWithDependenciesOfSameTypeInjectedThroughVarargsConstructorParameter() {
99          assertNotNull(support);
100 
101         new Expectations() {
102             {
103                 mock1.doSomething();
104                 result = 23;
105                 mock2.doSomething();
106                 result = 5;
107             }
108         };
109 
110         assertEquals(28, tested.doSomeOperation());
111     }
112 
113     /**
114      * Exercise tested object with dependencies provided by mock fields and mock parameter.
115      *
116      * @param mock3
117      *            the mock 3
118      */
119     @Test
120     void exerciseTestedObjectWithDependenciesProvidedByMockFieldsAndMockParameter(@Injectable final Dependency mock3) {
121         assertNotNull(support);
122 
123         new Expectations() {
124             {
125                 mock1.doSomething();
126                 result = 2;
127                 mock2.doSomething();
128                 result = 3;
129                 mock3.doSomething();
130                 result = 5;
131             }
132         };
133 
134         assertEquals(10, tested.doSomeOperation());
135     }
136 
137     /**
138      * The Class ClassWithStringParameter.
139      */
140     static class ClassWithStringParameter {
141 
142         /** The name. */
143         final String name;
144 
145         /**
146          * Instantiates a new class with string parameter.
147          *
148          * @param name
149          *            the name
150          */
151         ClassWithStringParameter(String name) {
152             this.name = name;
153         }
154     }
155 
156     /** The tested 2. */
157     @Tested
158     ClassWithStringParameter tested2;
159 
160     /** The name. */
161     @Injectable
162     String name;
163 
164     /**
165      * Initialize tested object with empty string parameter.
166      */
167     @Test
168     void initializeTestedObjectWithEmptyStringParameter() {
169         assertEquals("", tested2.name);
170     }
171 }