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