1
2
3
4
5
6 package mockit;
7
8 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
9 import static org.junit.jupiter.api.Assertions.assertEquals;
10 import static org.junit.jupiter.api.Assertions.assertFalse;
11 import static org.junit.jupiter.api.Assertions.assertNull;
12 import static org.junit.jupiter.api.Assertions.assertSame;
13
14 import edu.umd.cs.findbugs.annotations.NonNull;
15
16 import jakarta.annotation.Resource;
17
18 import java.lang.annotation.Annotation;
19
20 import mockit.integration.junit5.JMockitExtension;
21
22 import org.checkerframework.checker.index.qual.NonNegative;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25
26
27
28
29 @ExtendWith(JMockitExtension.class)
30 class MockedAnnotationsTest {
31
32
33
34
35 public @interface MyAnnotation {
36
37
38
39
40
41
42 String value();
43
44
45
46
47
48
49 boolean flag() default true;
50
51
52
53
54
55
56 String[] values() default {};
57 }
58
59
60
61
62
63
64
65 @Test
66 void specifyValuesForAnnotationAttributes(@Mocked final MyAnnotation a) {
67 assertSame(MyAnnotation.class, a.annotationType());
68
69 new Expectations() {
70 {
71 a.flag();
72 result = false;
73 a.value();
74 result = "test";
75 a.values();
76 returns("abc", "dEf");
77 }
78 };
79
80 assertFalse(a.flag());
81 assertEquals("test", a.value());
82 assertArrayEquals(new String[] { "abc", "dEf" }, a.values());
83 }
84
85
86
87
88
89
90
91 @Test
92 void verifyUsesOfAnnotationAttributes(@Mocked final MyAnnotation a) {
93 new Expectations() {
94 {
95 a.value();
96 result = "test";
97 times = 2;
98 a.values();
99 returns("abc", "dEf");
100 }
101 };
102
103
104
105 assertFalse(a.flag());
106
107 assertEquals("test", a.value());
108 assertArrayEquals(new String[] { "abc", "dEf" }, a.values());
109 a.value();
110
111 new FullVerifications() {
112 {
113
114 a.flag();
115 }
116 };
117 }
118
119
120
121
122 @Resource
123 public interface AnInterface {
124 }
125
126
127
128
129
130
131
132 @Test
133 void mockingAnAnnotatedPublicInterface(@Mocked AnInterface mock) {
134 Annotation[] mockClassAnnotations = mock.getClass().getAnnotations();
135
136 assertEquals(0, mockClassAnnotations.length);
137 }
138
139
140
141
142 static class ClassWithNullabilityAnnotations {
143
144
145
146
147
148
149
150
151
152
153
154 @NonNull
155 String doSomething(@NonNegative int i, @NonNull Object obj) {
156 return "";
157 }
158 }
159
160
161
162
163
164
165
166 @Test
167 void mockClassWithNullabilityAnnotations(@Injectable final ClassWithNullabilityAnnotations mock) {
168 new Expectations() {
169 {
170 mock.doSomething(anyInt, any);
171 result = "test";
172 }
173 };
174
175 assertEquals("test", mock.doSomething(123, "test"));
176 }
177
178
179
180
181 static final class ClassWithAnnotatedField {
182
183 @Resource(type = int.class)
184 Object aField;
185 }
186
187
188
189
190
191
192
193 @Test
194 void mockClassHavingFieldAnnotatedWithAttributeHavingAPrimitiveClassAsValue(@Mocked ClassWithAnnotatedField mock) {
195 assertNull(mock.aField);
196 }
197 }