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