|
4 | 4 | import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor;
|
5 | 5 | import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
|
6 | 6 | import com.fasterxml.jackson.databind.introspect.AnnotatedParameter;
|
7 |
| - |
| 7 | +import org.junit.Before; |
8 | 8 | import org.junit.Test;
|
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.mockito.BDDMockito; |
| 11 | +import org.mockito.Mock; |
| 12 | +import org.mockito.runners.MockitoJUnitRunner; |
9 | 13 |
|
10 | 14 | import java.lang.reflect.Constructor;
|
| 15 | +import java.lang.reflect.MalformedParametersException; |
11 | 16 | import java.lang.reflect.Method;
|
| 17 | +import java.lang.reflect.Parameter; |
12 | 18 |
|
13 |
| -import static org.junit.Assert.assertEquals; |
| 19 | +import static org.assertj.core.api.BDDAssertions.then; |
| 20 | +import static org.mockito.BDDMockito.given; |
| 21 | +import static org.mockito.Matchers.any; |
14 | 22 |
|
15 | 23 | /**
|
16 | 24 | * @author Lovro Pandzic
|
17 | 25 | */
|
| 26 | +@RunWith(MockitoJUnitRunner.class) |
18 | 27 | public class ParameterNamesAnnotationIntrospectorTest {
|
19 | 28 |
|
20 |
| - private final ParameterNamesAnnotationIntrospector PN_AI = new ParameterNamesAnnotationIntrospector(JsonCreator.Mode.DEFAULT); |
| 29 | + @Mock |
| 30 | + private ParameterExtractor parameterExtractor; |
| 31 | + |
| 32 | + private ParameterNamesAnnotationIntrospector introspector; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void setUp() throws Exception { |
| 36 | + introspector = new ParameterNamesAnnotationIntrospector(JsonCreator.Mode.DEFAULT, parameterExtractor); |
| 37 | + } |
21 | 38 |
|
22 | 39 | @Test
|
23 | 40 | public void shouldFindParameterNameFromConstructorForLegalIndex() throws Exception {
|
24 |
| - Constructor<?> ctor = ImmutableBean.class.getConstructor(String.class, Integer.class); |
25 |
| - |
26 |
| - assertEquals("name", ctor.getParameters()[0].getName()); |
27 | 41 |
|
28 |
| - AnnotatedConstructor owner = new AnnotatedConstructor(null, ctor, null, null); |
| 42 | + // given |
| 43 | + Constructor<?> givenConstructor = ImmutableBean.class.getConstructor(String.class, Integer.class); |
| 44 | + Parameter[] givenParameters = givenConstructor.getParameters(); |
| 45 | + AnnotatedConstructor owner = new AnnotatedConstructor(null, givenConstructor, null, null); |
29 | 46 | AnnotatedParameter annotatedParameter = new AnnotatedParameter(owner, null, null, 0);
|
30 |
| - |
31 |
| - String propertyName = PN_AI.findImplicitPropertyName(annotatedParameter); |
| 47 | + given(parameterExtractor.getParameters(any())).willReturn(givenParameters); |
| 48 | + |
| 49 | + // when |
| 50 | + String actual = introspector.findImplicitPropertyName(annotatedParameter); |
32 | 51 |
|
33 |
| - assertEquals("name", propertyName); |
| 52 | + then(actual).isEqualTo("name"); |
| 53 | + BDDMockito.then(parameterExtractor).should().getParameters(givenConstructor); |
34 | 54 | }
|
35 | 55 |
|
36 | 56 | @Test
|
37 | 57 | public void shouldFindParameterNameFromMethodForLegalIndex() throws Exception {
|
38 | 58 |
|
39 |
| - Method method = ImmutableBeanWithStaticFactory.class.getMethod("of", String.class, Integer.class); |
| 59 | + // given |
| 60 | + Method givenMethod = ImmutableBeanWithStaticFactory.class.getMethod("of", String.class, Integer.class); |
| 61 | + Parameter[] givenParameters = givenMethod.getParameters(); |
| 62 | + AnnotatedMethod owner = new AnnotatedMethod(null, givenMethod, null, null); |
| 63 | + AnnotatedParameter annotatedParameter = new AnnotatedParameter(owner, null, null, 0); |
| 64 | + given(parameterExtractor.getParameters(any())).willReturn(givenParameters); |
| 65 | + |
| 66 | + // when |
| 67 | + String actual = introspector.findImplicitPropertyName(annotatedParameter); |
| 68 | + |
| 69 | + // then |
| 70 | + then(actual).isEqualTo("name"); |
| 71 | + BDDMockito.then(parameterExtractor).should().getParameters(givenMethod); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void shouldReturnNullForMalformedParametersException() throws Exception { |
40 | 76 |
|
41 |
| - assertEquals("name", method.getParameters()[0].getName()); |
42 |
| - |
43 |
| - AnnotatedMethod owner = new AnnotatedMethod(null, method, null, null); |
| 77 | + // given |
| 78 | + Constructor<?> givenConstructor = ImmutableBean.class.getConstructor(String.class, Integer.class); |
| 79 | + AnnotatedConstructor owner = new AnnotatedConstructor(null, givenConstructor, null, null); |
44 | 80 | AnnotatedParameter annotatedParameter = new AnnotatedParameter(owner, null, null, 0);
|
| 81 | + given(parameterExtractor.getParameters(any())).willThrow(new MalformedParametersException()); |
45 | 82 |
|
46 |
| - String propertyName = PN_AI.findImplicitPropertyName(annotatedParameter); |
| 83 | + // when |
| 84 | + String actual = introspector.findImplicitPropertyName(annotatedParameter); |
47 | 85 |
|
48 |
| - assertEquals("name", propertyName); |
| 86 | + then(actual).isNull(); |
| 87 | + BDDMockito.then(parameterExtractor).should().getParameters(givenConstructor); |
49 | 88 | }
|
50 | 89 | }
|
0 commit comments