• Home
  • History
  • Annotate
  • 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.Before;
9  import org.junit.Test;
10  import org.mockito.Answers;
11  import org.mockito.Mock;
12  import org.mockito.MockitoAnnotations;
13  import org.mockito.exceptions.base.MockitoException;
14  import org.mockitousage.IMethods;
15  import org.mockitoutil.TestBase;
16  
17  import java.lang.annotation.Retention;
18  import java.lang.annotation.RetentionPolicy;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import static junit.framework.TestCase.*;
25  import static org.mockito.Mockito.verify;
26  
27  public class AnnotationsTest extends TestBase {
28  
29      @Retention(RetentionPolicy.RUNTIME)
30      public @interface NotAMock {}
31  
32      @Mock List<?> list;
33      @Mock final Map<Integer, String> map = new HashMap<Integer, String>();
34  
35      @NotAMock Set<?> notAMock;
36  
37      @Mock List<?> listTwo;
38  
39      @Before
setup()40      public void setup() {
41          MockitoAnnotations.initMocks(this);
42      }
43  
44      @Test
shouldInitMocks()45      public void shouldInitMocks() throws Exception {
46          list.clear();
47          map.clear();
48          listTwo.clear();
49  
50          verify(list).clear();
51          verify(map).clear();
52          verify(listTwo).clear();
53      }
54  
55      @Test
shouldScreamWhenInitializingMocksForNullClass()56      public void shouldScreamWhenInitializingMocksForNullClass() throws Exception {
57          try {
58              MockitoAnnotations.initMocks(null);
59              fail();
60          } catch (MockitoException e) {
61              assertEquals("testClass cannot be null. For info how to use @Mock annotations see examples in javadoc for MockitoAnnotations class",
62                      e.getMessage());
63          }
64      }
65  
66      @Test
shouldLookForAnnotatedMocksInSuperClasses()67      public void shouldLookForAnnotatedMocksInSuperClasses() throws Exception {
68          Sub sub = new Sub();
69          MockitoAnnotations.initMocks(sub);
70  
71          assertNotNull(sub.getMock());
72          assertNotNull(sub.getBaseMock());
73          assertNotNull(sub.getSuperBaseMock());
74      }
75  
76      @Mock(answer = Answers.RETURNS_MOCKS, name = "i have a name") IMethods namedAndReturningMocks;
77      @Mock(answer = Answers.RETURNS_DEFAULTS) IMethods returningDefaults;
78      @Mock(extraInterfaces = {List.class}) IMethods hasExtraInterfaces;
79      @Mock() IMethods noExtraConfig;
80  
81      @Test
shouldInitMocksWithGivenSettings()82      public void shouldInitMocksWithGivenSettings() throws Exception {
83          assertEquals("i have a name", namedAndReturningMocks.toString());
84          assertNotNull(namedAndReturningMocks.iMethodsReturningMethod());
85  
86          assertEquals("returningDefaults", returningDefaults.toString());
87          assertEquals(0, returningDefaults.intReturningMethod());
88  
89          assertTrue(hasExtraInterfaces instanceof List);
90  
91          assertEquals(0, noExtraConfig.intReturningMethod());
92      }
93  
94      class SuperBase {
95          @Mock private IMethods mock;
96  
getSuperBaseMock()97          public IMethods getSuperBaseMock() {
98              return mock;
99          }
100      }
101  
102      class Base extends SuperBase {
103          @Mock private IMethods mock;
104  
getBaseMock()105          public IMethods getBaseMock() {
106              return mock;
107          }
108      }
109  
110      class Sub extends Base {
111          @Mock private IMethods mock;
112  
getMock()113          public IMethods getMock() {
114              return mock;
115          }
116      }
117  }
118