View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertFalse;
4   import static org.junit.jupiter.api.Assertions.assertNotNull;
5   import static org.junit.jupiter.api.Assertions.assertNull;
6   import static org.junit.jupiter.api.Assertions.assertSame;
7   import static org.junit.jupiter.api.Assertions.assertTrue;
8   
9   import org.junit.jupiter.api.BeforeEach;
10  import org.junit.jupiter.api.Test;
11  
12  /**
13   * The Class TestedClassWithNoDITest.
14   */
15  final class TestedClassWithNoDITest {
16  
17      /**
18       * The Class TestedClass.
19       */
20      public static final class TestedClass {
21  
22          /** The dependency. */
23          private final Dependency dependency = new Dependency();
24  
25          /**
26           * Do some operation.
27           *
28           * @return true, if successful
29           */
30          public boolean doSomeOperation() {
31              return dependency.doSomething() > 0;
32          }
33      }
34  
35      /**
36       * The Class Dependency.
37       */
38      static class Dependency {
39          /**
40           * Do something.
41           *
42           * @return the int
43           */
44          int doSomething() {
45              return -1;
46          }
47      }
48  
49      /** The tested 1. */
50      @Tested
51      TestedClass tested1;
52  
53      /** The tested 2. */
54      @Tested
55      final TestedClass tested2 = new TestedClass();
56  
57      /** The tested 3. */
58      @Tested
59      TestedClass tested3;
60  
61      /** The tested 4. */
62      @Tested
63      NonPublicTestedClass tested4;
64  
65      /** The tested 5. */
66      @Tested
67      final TestedClass tested5 = null;
68  
69      /** The mock. */
70      @Mocked
71      Dependency mock;
72  
73      /** The tested. */
74      TestedClass tested;
75  
76      /**
77       * Sets the up.
78       */
79      @BeforeEach
80      void setUp() {
81          assertNotNull(mock);
82          assertNull(tested);
83          tested = new TestedClass();
84          assertNull(tested3);
85          tested3 = tested;
86          assertNull(tested1);
87          assertNotNull(tested2);
88          assertNull(tested4);
89          assertNull(tested5);
90      }
91  
92      /**
93       * Verify tested fields.
94       */
95      @Test
96      void verifyTestedFields() {
97          assertNull(tested5);
98          assertNotNull(tested4);
99          assertNotNull(tested3);
100         assertSame(tested, tested3);
101         assertNotNull(tested2);
102         assertNotNull(tested1);
103     }
104 
105     /**
106      * Exercise automatically instantiated tested object.
107      */
108     @Test
109     void exerciseAutomaticallyInstantiatedTestedObject() {
110         new Expectations() {
111             {
112                 mock.doSomething();
113                 result = 1;
114             }
115         };
116 
117         assertTrue(tested1.doSomeOperation());
118     }
119 
120     /**
121      * Exercise manually instantiated tested object.
122      */
123     @Test
124     void exerciseManuallyInstantiatedTestedObject() {
125         new Expectations() {
126             {
127                 mock.doSomething();
128                 result = 1;
129             }
130         };
131 
132         assertTrue(tested2.doSomeOperation());
133 
134         new FullVerifications() {
135         };
136     }
137 
138     /**
139      * Exercise another manually instantiated tested object.
140      */
141     @Test
142     void exerciseAnotherManuallyInstantiatedTestedObject() {
143         assertFalse(tested3.doSomeOperation());
144 
145         new Verifications() {
146             {
147                 mock.doSomething();
148                 times = 1;
149             }
150         };
151     }
152 }
153 
154 class NonPublicTestedClass {
155     @SuppressWarnings("RedundantNoArgConstructor")
156     NonPublicTestedClass() {
157     }
158 }