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