View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertTrue;
5   
6   import mockit.internal.expectations.invocation.MissingInvocation;
7   
8   import org.junit.Test;
9   
10  /**
11   * The Class TestedAndMockedTest.
12   */
13  public final class TestedAndMockedTest {
14  
15      /**
16       * The Class ClassToBeTested.
17       */
18      public static class ClassToBeTested {
19  
20          /** The some data. */
21          private final String someData;
22  
23          /** The output data. */
24          String outputData;
25  
26          /** The collaborator. */
27          AnotherClassToBeTested collaborator;
28  
29          /**
30           * Instantiates a new class to be tested.
31           *
32           * @param someData
33           *            the some data
34           */
35          public ClassToBeTested(String someData) {
36              this.someData = someData;
37          }
38  
39          /**
40           * Do some operation.
41           *
42           * @param i
43           *            the i
44           * @param s
45           *            the s
46           *
47           * @return true, if successful
48           */
49          public boolean doSomeOperation(int i, String s) {
50              validateInput(i, s);
51              int j = i + doSomething();
52              doSomethingElse(s);
53              return j > 0;
54          }
55  
56          /**
57           * Validate input.
58           *
59           * @param i
60           *            the i
61           * @param s
62           *            the s
63           */
64          static void validateInput(int i, String s) {
65              if (i <= 0 || s == null) {
66                  throw new IllegalArgumentException();
67              }
68          }
69  
70          /**
71           * Do something.
72           *
73           * @return the int
74           */
75          int doSomething() {
76              return -1;
77          }
78  
79          /**
80           * Do something else.
81           *
82           * @param s
83           *            the s
84           */
85          void doSomethingElse(String s) {
86              outputData = "output data: " + s;
87          }
88  
89          /**
90           * Do another operation.
91           *
92           * @return the int
93           */
94          int doAnotherOperation() {
95              return collaborator.doSomething() - 23;
96          }
97      }
98  
99      /**
100      * The Class AnotherClassToBeTested.
101      */
102     static final class AnotherClassToBeTested {
103         /**
104          * Do something.
105          *
106          * @return the int
107          */
108         int doSomething() {
109             return 123;
110         }
111     }
112 
113     /** The tested and injected. */
114     @Tested
115     AnotherClassToBeTested testedAndInjected;
116 
117     /** The tested. */
118     @Tested(fullyInitialized = true)
119     @Mocked
120     ClassToBeTested tested;
121 
122     /** The test data. */
123     @Injectable
124     final String testData = "test data";
125 
126     /**
127      * Exercise public method while having helper methods mocked.
128      */
129     @Test
130     public void exercisePublicMethodWhileHavingHelperMethodsMocked() {
131         assertEquals(testData, tested.someData);
132 
133         new Expectations() {
134             {
135                 tested.doSomething();
136                 result = 123;
137             }
138         };
139         new Expectations() {
140             {
141                 ClassToBeTested.validateInput(anyInt, anyString);
142             }
143         };
144 
145         boolean result = tested.doSomeOperation(0, "testing");
146 
147         assertTrue(result);
148         assertEquals("output data: testing", tested.outputData);
149 
150         new Verifications() {
151             {
152                 tested.doSomethingElse(anyString);
153                 times = 1;
154             }
155         };
156     }
157 
158     /**
159      * Exercise top level tested object together with injected second level tested object.
160      */
161     @Test
162     public void exerciseTopLevelTestedObjectTogetherWithInjectedSecondLevelTestedObject() {
163         assertEquals(123, testedAndInjected.doSomething());
164         assertEquals(100, tested.doAnotherOperation());
165     }
166 
167     /**
168      * Mock tested class.
169      *
170      * @param mock
171      *            the mock
172      */
173     @Test(expected = MissingInvocation.class)
174     public void mockTestedClass(@Mocked final ClassToBeTested mock) {
175         new Expectations() {
176             {
177                 mock.doSomethingElse("");
178             }
179         };
180     }
181 }