View Javadoc
1   package otherTests;
2   
3   import static org.junit.jupiter.api.Assertions.assertFalse;
4   
5   import mockit.Capturing;
6   import mockit.Expectations;
7   import mockit.Mocked;
8   
9   import org.junit.jupiter.api.BeforeEach;
10  import org.junit.jupiter.api.MethodOrderer.MethodName;
11  import org.junit.jupiter.api.Test;
12  import org.junit.jupiter.api.TestMethodOrder;
13  
14  /**
15   * The Class SubclassTest.
16   */
17  @TestMethodOrder(MethodName.class)
18  final class SubclassTest {
19  
20      /** The super class constructor called. */
21      private static boolean superClassConstructorCalled;
22  
23      /** The sub class constructor called. */
24      private static boolean subClassConstructorCalled;
25  
26      /**
27       * The Class SuperClass.
28       */
29      public static class SuperClass {
30  
31          /** The name. */
32          final String name;
33  
34          /**
35           * Instantiates a new super class.
36           *
37           * @param x
38           *            the x
39           * @param name
40           *            the name
41           */
42          public SuperClass(int x, String name) {
43              this.name = name + x;
44              superClassConstructorCalled = true;
45          }
46      }
47  
48      /**
49       * The Class SubClass.
50       */
51      public static class SubClass extends SuperClass {
52  
53          /**
54           * Instantiates a new sub class.
55           *
56           * @param name
57           *            the name
58           */
59          public SubClass(String name) {
60              super(name.length(), name);
61              subClassConstructorCalled = true;
62          }
63      }
64  
65      /**
66       * Sets the up.
67       */
68      @BeforeEach
69      void setUp() {
70          superClassConstructorCalled = false;
71          subClassConstructorCalled = false;
72      }
73  
74      /**
75       * Capture subclass through classfile transformer.
76       *
77       * @param captured
78       *            the captured
79       */
80      @Test
81      void captureSubclassThroughClassfileTransformer(@Capturing SuperClass captured) {
82          new SubClass("capture");
83  
84          assertFalse(superClassConstructorCalled);
85          assertFalse(subClassConstructorCalled);
86      }
87  
88      /**
89       * Capture subclass through redefinition of previously loaded classes.
90       *
91       * @param captured
92       *            the captured
93       */
94      @Test
95      void captureSubclassThroughRedefinitionOfPreviouslyLoadedClasses(@Capturing SuperClass captured) {
96          new SubClass("capture");
97  
98          assertFalse(superClassConstructorCalled);
99          assertFalse(subClassConstructorCalled);
100     }
101 
102     /**
103      * Mock subclass using expectations with first super constructor.
104      *
105      * @param mock
106      *            the mock
107      */
108     @Test
109     void mockSubclassUsingExpectationsWithFirstSuperConstructor(@Mocked SubClass mock) {
110         new Expectations() {
111             {
112                 new SubClass("test");
113             }
114         };
115 
116         new SubClass("test");
117 
118         assertFalse(superClassConstructorCalled);
119         assertFalse(subClassConstructorCalled);
120     }
121 }