View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   
5   import org.junit.jupiter.api.Test;
6   
7   /**
8    * The Class ReentrantDelegateTest.
9    */
10  final class ReentrantDelegateTest {
11  
12      /**
13       * The Class RealClass.
14       */
15      public static class RealClass {
16  
17          /**
18           * Non recursive static method.
19           *
20           * @param i
21           *            the i
22           *
23           * @return the int
24           */
25          protected static int nonRecursiveStaticMethod(int i) {
26              return -i;
27          }
28  
29          /**
30           * Non recursive method.
31           *
32           * @param i
33           *            the i
34           *
35           * @return the int
36           */
37          public int nonRecursiveMethod(int i) {
38              return -i;
39          }
40      }
41  
42      /**
43       * Recursive delegate method without invocation parameter.
44       *
45       * @param mock
46       *            the mock
47       */
48      @Test
49      void recursiveDelegateMethodWithoutInvocationParameter(@Mocked RealClass mock) {
50          new Expectations() {
51              {
52                  RealClass.nonRecursiveStaticMethod(anyInt);
53                  result = new Delegate<Object>() {
54                      @Mock
55                      int delegate(int i) {
56                          if (i > 1) {
57                              return i;
58                          }
59                          return RealClass.nonRecursiveStaticMethod(i + 1);
60                      }
61                  };
62              }
63          };
64  
65          int result = RealClass.nonRecursiveStaticMethod(1);
66          assertEquals(2, result);
67      }
68  
69      /**
70       * Recursive delegate method with invocation parameter not used for proceeding.
71       *
72       * @param rc
73       *            the rc
74       */
75      @Test
76      void recursiveDelegateMethodWithInvocationParameterNotUsedForProceeding(@Injectable final RealClass rc) {
77          new Expectations() {
78              {
79                  rc.nonRecursiveMethod(anyInt);
80                  result = new Delegate<Object>() {
81                      @Mock
82                      int delegate(Invocation inv, int i) {
83                          if (i > 1) {
84                              return i;
85                          }
86                          RealClass it = inv.getInvokedInstance();
87                          return it.nonRecursiveMethod(i + 1);
88                      }
89                  };
90              }
91          };
92  
93          int result = rc.nonRecursiveMethod(1);
94          assertEquals(2, result);
95      }
96  
97      /**
98       * Non recursive delegate method with invocation parameter used for proceeding.
99       *
100      * @param rc
101      *            the rc
102      */
103     @Test
104     void nonRecursiveDelegateMethodWithInvocationParameterUsedForProceeding(@Injectable final RealClass rc) {
105         new Expectations() {
106             {
107                 rc.nonRecursiveMethod(anyInt);
108                 result = new Delegate<Object>() {
109                     @Mock
110                     int nonRecursiveMethod(Invocation inv, int i) {
111                         if (i > 1) {
112                             return i;
113                         }
114                         return inv.proceed(i + 1);
115                     }
116                 };
117             }
118         };
119 
120         int result = rc.nonRecursiveMethod(1);
121         assertEquals(-2, result);
122     }
123 }