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.assertTrue;
9   
10  import org.junit.jupiter.api.AfterEach;
11  import org.junit.jupiter.api.BeforeEach;
12  import org.junit.jupiter.api.Test;
13  
14  /**
15   * The Class DynamicMockingInBeforeMethodTest.
16   */
17  final class DynamicMockingInBeforeMethodTest {
18  
19      /**
20       * The Class MockedClass.
21       */
22      static final class MockedClass {
23          /**
24           * Do something.
25           *
26           * @param i
27           *            the i
28           *
29           * @return true, if successful
30           */
31          boolean doSomething(int i) {
32              return i > 0;
33          }
34      }
35  
36      /** The an instance. */
37      final MockedClass anInstance = new MockedClass();
38  
39      /**
40       * Record expectations on dynamically mocked class.
41       */
42      @BeforeEach
43      void recordExpectationsOnDynamicallyMockedClass() {
44          assertTrue(anInstance.doSomething(56));
45  
46          new Expectations(anInstance) {
47              {
48                  anInstance.doSomething(anyInt);
49                  result = true;
50                  minTimes = 0;
51              }
52          };
53  
54          assertTrue(anInstance.doSomething(-56));
55      }
56  
57      /**
58       * Verify that dynamically mocked class is still mocked.
59       */
60      @AfterEach
61      void verifyThatDynamicallyMockedClassIsStillMocked() {
62          new FullVerifications() {
63              {
64                  anInstance.doSomething(anyInt);
65                  times = 2;
66              }
67          };
68      }
69  
70      /**
71       * Test something.
72       */
73      @Test
74      void testSomething() {
75          assertTrue(anInstance.doSomething(56));
76      }
77  
78      /**
79       * Test something else.
80       */
81      @Test
82      void testSomethingElse() {
83          assertTrue(anInstance.doSomething(-129));
84      }
85  }