• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockitousage.annotation;
7 
8 import org.junit.Test;
9 import org.mockito.*;
10 import org.mockito.exceptions.base.MockitoException;
11 import org.mockitousage.IMethods;
12 import org.mockitoutil.TestBase;
13 
14 import java.lang.annotation.Retention;
15 import java.lang.annotation.RetentionPolicy;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Set;
19 
20 import static junit.framework.TestCase.*;
21 import static org.assertj.core.api.Assertions.assertThat;
22 
23 public class CaptorAnnotationTest extends TestBase {
24 
25     @Retention(RetentionPolicy.RUNTIME)
26     public @interface NotAMock {
27     }
28 
29     @Captor
30     final ArgumentCaptor<String> finalCaptor = ArgumentCaptor.forClass(String.class);
31 
32     @Captor
33     ArgumentCaptor<List<List<String>>> genericsCaptor;
34 
35     @SuppressWarnings("rawtypes")
36     @Captor
37     ArgumentCaptor nonGenericCaptorIsAllowed;
38 
39     @Mock
40     MockInterface mockInterface;
41 
42     @NotAMock
43     Set<?> notAMock;
44 
45     public interface MockInterface {
testMe(String simple, List<List<String>> genericList)46         void testMe(String simple, List<List<String>> genericList);
47     }
48 
49     @Test
testNormalUsage()50     public void testNormalUsage() {
51 
52         MockitoAnnotations.initMocks(this);
53 
54         // check if assigned correctly
55         assertNotNull(finalCaptor);
56         assertNotNull(genericsCaptor);
57         assertNotNull(nonGenericCaptorIsAllowed);
58         assertNull(notAMock);
59 
60         // use captors in the field to be sure they are cool
61         String argForFinalCaptor = "Hello";
62         ArrayList<List<String>> argForGenericsCaptor = new ArrayList<List<String>>();
63 
64         mockInterface.testMe(argForFinalCaptor, argForGenericsCaptor);
65 
66         Mockito.verify(mockInterface).testMe(finalCaptor.capture(), genericsCaptor.capture());
67 
68         assertEquals(argForFinalCaptor, finalCaptor.getValue());
69         assertEquals(argForGenericsCaptor, genericsCaptor.getValue());
70 
71     }
72 
73     public static class WrongType {
74         @Captor
75         List<?> wrongType;
76     }
77 
78     @Test
shouldScreamWhenWrongTypeForCaptor()79     public void shouldScreamWhenWrongTypeForCaptor() {
80         try {
81             MockitoAnnotations.initMocks(new WrongType());
82             fail();
83         } catch (MockitoException e) {}
84     }
85 
86     public static class ToManyAnnotations {
87         @Captor
88         @Mock
89         ArgumentCaptor<List> missingGenericsField;
90     }
91 
92     @Test
shouldScreamWhenMoreThanOneMockitoAnnotaton()93     public void shouldScreamWhenMoreThanOneMockitoAnnotaton() {
94         try {
95             MockitoAnnotations.initMocks(new ToManyAnnotations());
96             fail();
97         } catch (MockitoException e) {
98             assertThat(e)
99                 .hasMessageContaining("missingGenericsField")
100                 .hasMessageContaining("multiple Mockito annotations");
101         }
102     }
103 
104     @Test
shouldScreamWhenInitializingCaptorsForNullClass()105     public void shouldScreamWhenInitializingCaptorsForNullClass() throws Exception {
106         try {
107             MockitoAnnotations.initMocks(null);
108             fail();
109         } catch (MockitoException e) {
110         }
111     }
112 
113     @Test
shouldLookForAnnotatedCaptorsInSuperClasses()114     public void shouldLookForAnnotatedCaptorsInSuperClasses() throws Exception {
115         Sub sub = new Sub();
116         MockitoAnnotations.initMocks(sub);
117 
118         assertNotNull(sub.getCaptor());
119         assertNotNull(sub.getBaseCaptor());
120         assertNotNull(sub.getSuperBaseCaptor());
121     }
122 
123     class SuperBase {
124         @Captor
125         private ArgumentCaptor<IMethods> mock;
126 
getSuperBaseCaptor()127         public ArgumentCaptor<IMethods> getSuperBaseCaptor() {
128             return mock;
129         }
130     }
131 
132     class Base extends SuperBase {
133         @Captor
134         private ArgumentCaptor<IMethods> mock;
135 
getBaseCaptor()136         public ArgumentCaptor<IMethods> getBaseCaptor() {
137             return mock;
138         }
139     }
140 
141     class Sub extends Base {
142         @Captor
143         private ArgumentCaptor<IMethods> mock;
144 
getCaptor()145         public ArgumentCaptor<IMethods> getCaptor() {
146             return mock;
147         }
148     }
149 }
150