/
MockAnnotationProcessor.java
92 lines (84 loc) · 3.47 KB
/
MockAnnotationProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import static org.mockito.internal.util.StringUtil.join;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import org.mockito.Mock;
import org.mockito.MockSettings;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.util.Supplier;
/**
* Instantiates a mock on a field annotated by {@link Mock}
*/
public class MockAnnotationProcessor implements FieldAnnotationProcessor<Mock> {
@Override
public Object process(Mock annotation, Field field) {
return processAnnotationForMock(
annotation, field.getType(), field::getGenericType, field.getName());
}
public static Object processAnnotationForMock(
Mock annotation, Class<?> type, Supplier<Type> genericType, String name) {
MockSettings mockSettings = Mockito.withSettings();
if (annotation.extraInterfaces().length > 0) { // never null
mockSettings.extraInterfaces(annotation.extraInterfaces());
}
if ("".equals(annotation.name())) {
mockSettings.name(name);
} else {
mockSettings.name(annotation.name());
}
if (annotation.serializable()) {
mockSettings.serializable();
}
if (annotation.stubOnly()) {
mockSettings.stubOnly();
}
mockSettings.strictness(annotation.strictness());
if (annotation.lenient()) {
mockSettings.lenient();
}
// see @Mock answer default value
mockSettings.defaultAnswer(annotation.answer());
if (type == MockedStatic.class) {
return Mockito.mockStatic(
inferParameterizedType(
genericType.get(), name, MockedStatic.class.getSimpleName()),
mockSettings);
} else if (type == MockedConstruction.class) {
return Mockito.mockConstruction(
inferParameterizedType(
genericType.get(), name, MockedConstruction.class.getSimpleName()),
mockSettings);
} else {
return Mockito.mock(type, mockSettings);
}
}
static Class<?> inferParameterizedType(Type type, String name, String sort) {
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] arguments = parameterizedType.getActualTypeArguments();
if (arguments.length == 1) {
if (arguments[0] instanceof Class<?>) {
return (Class<?>) arguments[0];
}
}
}
throw new MockitoException(
join(
"Mockito cannot infer a static mock from a raw type for " + name,
"",
"Instead of @Mock " + sort + " you need to specify a parameterized type",
"For example, if you would like to mock Sample.class, specify",
"",
"@Mock " + sort + "<Sample>",
"",
"as the type parameter. If the type is itself parameterized, it should be specified as raw type."));
}
}