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.assertNotNull;
11  import static org.junit.jupiter.api.Assertions.assertNull;
12  import static org.junit.jupiter.api.Assertions.assertSame;
13  import static org.junit.jupiter.api.Assertions.assertTrue;
14  
15  import mockit.integration.junit5.JMockitExtension;
16  
17  import org.junit.jupiter.api.Test;
18  import org.junit.jupiter.api.extension.ExtendWith;
19  
20  @ExtendWith(JMockitExtension.class)
21  class MockInvocationTest {
22  
23      public static class Collaborator {
24          int value;
25  
26          Collaborator() {
27          }
28  
29          public Collaborator(int i) {
30              value = i;
31          }
32  
33          public int getValue() {
34              return -1;
35          }
36  
37          public void setValue(int i) {
38              value = i;
39          }
40  
41          public String doSomething(boolean b, int[] i, String s) {
42              return s + b + i[0];
43          }
44  
45          public static boolean staticMethod() {
46              return true;
47          }
48      }
49  
50      static final class MockMethods extends MockUp<Collaborator> {
51          @Mock
52          static boolean staticMethod(Invocation context) {
53              assertNotNull(context);
54              assertNull(context.getInvokedInstance());
55              assertEquals(1, context.getInvocationCount());
56              return false;
57          }
58  
59          @Mock
60          int getValue(Invocation context) {
61              assertTrue(context.getInvokedInstance() instanceof Collaborator);
62              assertEquals(0, context.getInvocationIndex());
63              return 123;
64          }
65      }
66  
67      @Test
68      void mockMethodsForMethodsWithoutParameters() {
69          new MockMethods();
70          assertFalse(Collaborator.staticMethod());
71          assertEquals(123, new Collaborator().getValue());
72      }
73  
74      @Test
75      void instanceMockMethodForStaticMethod() {
76          new MockUp<Collaborator>() {
77              @Mock
78              boolean staticMethod(Invocation context) {
79                  assertNull(context.getInvokedInstance());
80                  assertEquals(context.getInvocationCount() - 1, context.getInvocationIndex());
81                  return context.getInvocationCount() <= 0;
82              }
83          };
84  
85          assertFalse(Collaborator.staticMethod());
86          assertFalse(Collaborator.staticMethod());
87      }
88  
89      @Test
90      void mockMethodsWithInvocationParameter() {
91          new MockUp<Collaborator>() {
92              Collaborator instantiated;
93  
94              @Mock
95              void $init(Invocation inv, int i) {
96                  assertNotNull(inv.getInvokedInstance());
97                  assertTrue(i > 0);
98                  instantiated = inv.getInvokedInstance();
99              }
100 
101             @Mock
102             String doSomething(Invocation inv, boolean b, int[] array, String s) {
103                 assertNotNull(inv);
104                 assertSame(instantiated, inv.getInvokedInstance());
105                 assertEquals(1, inv.getInvocationCount());
106                 assertTrue(b);
107                 assertNull(array);
108                 assertEquals("test", s);
109                 return "mock";
110             }
111         };
112 
113         String s = new Collaborator(123).doSomething(true, null, "test");
114         assertEquals("mock", s);
115     }
116 
117     static class MockMethodsWithParameters extends MockUp<Collaborator> {
118         int capturedArgument;
119         Collaborator mockedInstance;
120 
121         @Mock
122         void $init(Invocation context, int i) {
123             capturedArgument = i + context.getInvocationCount();
124             assertNull(mockedInstance);
125             assertTrue(context.getInvokedInstance() instanceof Collaborator);
126             assertEquals(1, context.getInvokedArguments().length);
127         }
128 
129         @Mock
130         void setValue(Invocation context, int i) {
131             assertEquals(i, context.getInvocationIndex());
132             assertSame(mockedInstance, context.getInvokedInstance());
133             assertEquals(1, context.getInvokedArguments().length);
134         }
135     }
136 
137     @Test
138     void mockMethodsWithParameters() {
139         MockMethodsWithParameters mock = new MockMethodsWithParameters();
140 
141         Collaborator col = new Collaborator(4);
142         mock.mockedInstance = col;
143 
144         assertEquals(5, mock.capturedArgument);
145         col.setValue(0);
146         col.setValue(1);
147     }
148 
149     @Test
150     void useOfContextParametersForJREMethods() throws Exception {
151         new MockUp<Runtime>() {
152             @Mock
153             void runFinalizersOnExit(Invocation inv, boolean b) {
154                 assertNull(inv.getInvokedInstance());
155                 assertEquals(1, inv.getInvocationCount());
156                 assertTrue(b);
157             }
158 
159             @Mock
160             Process exec(Invocation inv, String command, String[] envp) {
161                 assertSame(Runtime.getRuntime(), inv.getInvokedInstance());
162                 assertEquals(0, inv.getInvocationIndex());
163                 assertNotNull(command);
164                 assertNull(envp);
165                 return null;
166             }
167         };
168 
169         // Runtime.runFinalizersOnExit(true);
170         assertNull(Runtime.getRuntime().exec("test", null));
171     }
172 }