1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4
5 import org.junit.jupiter.api.Test;
6
7
8
9
10 final class ReentrantDelegateTest {
11
12
13
14
15 public static class RealClass {
16
17
18
19
20
21
22
23
24
25 protected static int nonRecursiveStaticMethod(int i) {
26 return -i;
27 }
28
29
30
31
32
33
34
35
36
37 public int nonRecursiveMethod(int i) {
38 return -i;
39 }
40 }
41
42
43
44
45
46
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
71
72
73
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
99
100
101
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 }