• 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 package org.mockitousage.annotation;
6 
7 import static org.assertj.core.api.Assertions.assertThatThrownBy;
8 import static org.junit.Assert.fail;
9 
10 import java.util.List;
11 
12 import org.assertj.core.api.Assertions;
13 import org.junit.Test;
14 import org.mockito.*;
15 import org.mockito.exceptions.base.MockitoException;
16 import org.mockitoutil.TestBase;
17 
18 public class WrongSetOfAnnotationsTest extends TestBase {
19 
20     @Test
should_not_allow_Mock_and_Spy()21     public void should_not_allow_Mock_and_Spy() {
22         assertThatThrownBy(
23                         () -> {
24                             MockitoAnnotations.openMocks(
25                                     new Object() {
26                                         @Mock @Spy List<?> mock;
27                                     });
28                         })
29                 .isInstanceOf(MockitoException.class)
30                 .hasMessage(
31                         "This combination of annotations is not permitted on a single field:\n"
32                                 + "@Spy and @Mock");
33     }
34 
35     @Test
should_not_allow_Spy_and_InjectMocks_on_interfaces()36     public void should_not_allow_Spy_and_InjectMocks_on_interfaces() {
37         try {
38             MockitoAnnotations.openMocks(
39                     new Object() {
40                         @InjectMocks @Spy List<?> mock;
41                     });
42             fail();
43         } catch (MockitoException me) {
44             Assertions.assertThat(me.getMessage()).contains("'List' is an interface");
45         }
46     }
47 
48     @Test
should_allow_Spy_and_InjectMocks()49     public void should_allow_Spy_and_InjectMocks() {
50         MockitoAnnotations.openMocks(
51                 new Object() {
52                     @InjectMocks @Spy WithDependency mock;
53                 });
54     }
55 
56     static class WithDependency {
57         List<?> list;
58     }
59 
60     @Test
should_not_allow_Mock_and_InjectMocks()61     public void should_not_allow_Mock_and_InjectMocks() {
62         assertThatThrownBy(
63                         () -> {
64                             MockitoAnnotations.openMocks(
65                                     new Object() {
66                                         @InjectMocks @Mock List<?> mock;
67                                     });
68                         })
69                 .isInstanceOf(MockitoException.class)
70                 .hasMessage(
71                         "This combination of annotations is not permitted on a single field:\n"
72                                 + "@Mock and @InjectMocks");
73     }
74 
75     @Test
should_not_allow_Captor_and_Mock()76     public void should_not_allow_Captor_and_Mock() {
77         assertThatThrownBy(
78                         () -> {
79                             MockitoAnnotations.openMocks(
80                                     new Object() {
81                                         @Mock @Captor ArgumentCaptor<?> captor;
82                                     });
83                         })
84                 .isInstanceOf(MockitoException.class)
85                 .hasMessageContainingAll(
86                         "You cannot have more than one Mockito annotation on a field!",
87                         "The field 'captor' has multiple Mockito annotations.",
88                         "For info how to use annotations see examples in javadoc for MockitoAnnotations class.");
89     }
90 
91     @Test
should_not_allow_Captor_and_Spy()92     public void should_not_allow_Captor_and_Spy() {
93         assertThatThrownBy(
94                         () -> {
95                             MockitoAnnotations.openMocks(
96                                     new Object() {
97                                         @Spy @Captor ArgumentCaptor<?> captor;
98                                     });
99                         })
100                 .isInstanceOf(MockitoException.class)
101                 .hasMessage(
102                         "This combination of annotations is not permitted on a single field:\n"
103                                 + "@Spy and @Captor");
104     }
105 
106     @Test
should_not_allow_Captor_and_InjectMocks()107     public void should_not_allow_Captor_and_InjectMocks() {
108         assertThatThrownBy(
109                         () -> {
110                             MockitoAnnotations.openMocks(
111                                     new Object() {
112                                         @InjectMocks @Captor ArgumentCaptor<?> captor;
113                                     });
114                         })
115                 .isInstanceOf(MockitoException.class)
116                 .hasMessage(
117                         "This combination of annotations is not permitted on a single field:\n"
118                                 + "@Captor and @InjectMocks");
119     }
120 }
121