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.assertFalse;
10  import static org.junit.jupiter.api.Assertions.assertSame;
11  import static org.junit.jupiter.api.Assertions.assertTrue;
12  
13  import mockit.integration.junit5.JMockitExtension;
14  
15  import org.junit.jupiter.api.Test;
16  import org.junit.jupiter.api.extension.ExtendWith;
17  
18  /**
19   * The Class TestedAbstractClassTest.
20   */
21  @ExtendWith(JMockitExtension.class)
22  class TestedAbstractClassTest {
23  
24      /**
25       * The Class AbstractClass.
26       */
27      public abstract static class AbstractClass implements Runnable {
28  
29          /** The value. */
30          private final int value;
31  
32          /** The name. */
33          protected String name;
34  
35          /**
36           * Instantiates a new abstract class.
37           *
38           * @param value
39           *            the value
40           */
41          protected AbstractClass(int value) {
42              this.value = value;
43          }
44  
45          /**
46           * Do some operation.
47           *
48           * @return true, if successful
49           */
50          public final boolean doSomeOperation() {
51              run();
52              return doSomething() > 0;
53          }
54  
55          /**
56           * Do something.
57           *
58           * @return the int
59           */
60          protected abstract int doSomething();
61  
62          /**
63           * Gets the value.
64           *
65           * @return the value
66           */
67          public int getValue() {
68              return value;
69          }
70      }
71  
72      // A subclass is generated with the *same* constructors as the tested class, and with *mocked* implementations
73      /** The tested. */
74      // for all abstract methods in the tested base class, its super-classes and its implemented interfaces.
75      @Tested
76      AbstractClass tested;
77  
78      /** The value. */
79      @Injectable("123")
80      int value;
81  
82      /**
83       * Exercise tested object.
84       *
85       * @param name
86       *            the name
87       */
88      @Test
89      void exerciseTestedObject(@Injectable("Test") String name) {
90          assertThatGeneratedSubclassIsAlwaysTheSame();
91          assertEquals(123, tested.getValue());
92          assertEquals("Test", tested.name);
93  
94          new Expectations() {
95              {
96                  tested.doSomething();
97                  result = 23;
98                  times = 1;
99              }
100         };
101 
102         assertTrue(tested.doSomeOperation());
103 
104         new Verifications() {
105             {
106                 tested.run();
107             }
108         };
109     }
110 
111     /**
112      * Exercise dynamically mocked tested object.
113      */
114     @Test
115     void exerciseDynamicallyMockedTestedObject() {
116         assertThatGeneratedSubclassIsAlwaysTheSame();
117         assertEquals(123, tested.getValue());
118 
119         new Expectations(tested) {
120             {
121                 tested.getValue();
122                 result = 45;
123                 tested.doSomething();
124                 result = 7;
125             }
126         };
127 
128         assertEquals(45, tested.getValue());
129         assertTrue(tested.doSomeOperation());
130 
131         new Verifications() {
132             {
133                 tested.run();
134                 times = 1;
135             }
136         };
137     }
138 
139     /**
140      * Exercise tested object again.
141      *
142      * @param text
143      *            the text
144      */
145     @Test
146     void exerciseTestedObjectAgain(@Injectable("Another test") String text) {
147         assertThatGeneratedSubclassIsAlwaysTheSame();
148         assertEquals(123, tested.getValue());
149         assertEquals("Another test", tested.name);
150 
151         assertFalse(tested.doSomeOperation());
152 
153         new VerificationsInOrder() {
154             {
155                 tested.run();
156                 tested.doSomething();
157             }
158         };
159     }
160 
161     /** The generated subclass. */
162     Class<?> generatedSubclass;
163 
164     /**
165      * Assert that generated subclass is always the same.
166      */
167     void assertThatGeneratedSubclassIsAlwaysTheSame() {
168         Class<?> testedClass = tested.getClass();
169 
170         if (generatedSubclass == null) {
171             generatedSubclass = testedClass;
172         } else {
173             assertSame(generatedSubclass, testedClass);
174         }
175     }
176 }