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