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