• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019 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.mockito.Mockito.mock;
9 
10 import org.junit.Test;
11 import org.mockito.DoNotMock;
12 import org.mockito.Mock;
13 import org.mockito.MockitoAnnotations;
14 import org.mockito.exceptions.misusing.DoNotMockException;
15 
16 public class DoNotMockTest {
17 
18     @Test
can_not_mock_class_annotated_with_donotmock()19     public void can_not_mock_class_annotated_with_donotmock() {
20         assertThatThrownBy(
21                         () -> {
22                             NotMockable notMockable = mock(NotMockable.class);
23                         })
24                 .isInstanceOf(DoNotMockException.class);
25     }
26 
27     @Test
can_not_mock_class_via_mock_annotation()28     public void can_not_mock_class_via_mock_annotation() {
29         assertThatThrownBy(
30                         () -> {
31                             MockitoAnnotations.openMocks(new TestClass());
32                         })
33                 .isInstanceOf(DoNotMockException.class);
34     }
35 
36     @Test
can_not_mock_class_with_interface_annotated_with_donotmock()37     public void can_not_mock_class_with_interface_annotated_with_donotmock() {
38         assertThatThrownBy(
39                         () -> {
40                             SubclassOfNotMockableInterface notMockable =
41                                     mock(SubclassOfNotMockableInterface.class);
42                         })
43                 .isInstanceOf(DoNotMockException.class);
44     }
45 
46     @Test
can_not_mock_subclass_with_unmockable_interface()47     public void can_not_mock_subclass_with_unmockable_interface() {
48         assertThatThrownBy(
49                         () -> {
50                             DoubleSubclassOfInterface notMockable =
51                                     mock(DoubleSubclassOfInterface.class);
52                         })
53                 .isInstanceOf(DoNotMockException.class);
54     }
55 
56     @Test
can_not_mock_subclass_with_unmockable_superclass()57     public void can_not_mock_subclass_with_unmockable_superclass() {
58         assertThatThrownBy(
59                         () -> {
60                             SubclassOfNotMockableSuperclass notMockable =
61                                     mock(SubclassOfNotMockableSuperclass.class);
62                         })
63                 .isInstanceOf(DoNotMockException.class);
64     }
65 
66     @Test
67     public void
can_not_mock_subclass_with_unmockable_interface_that_extends_non_mockable_interface()68             can_not_mock_subclass_with_unmockable_interface_that_extends_non_mockable_interface() {
69         assertThatThrownBy(
70                         () -> {
71                             SubclassOfSubInterfaceOfNotMockableInterface notMockable =
72                                     mock(SubclassOfSubInterfaceOfNotMockableInterface.class);
73                         })
74                 .isInstanceOf(DoNotMockException.class);
75     }
76 
77     @Test
thrown_exception_includes_non_mockable_reason()78     public void thrown_exception_includes_non_mockable_reason() {
79         assertThatThrownBy(
80                         () -> {
81                             NotMockable notMockable = mock(NotMockable.class);
82                         })
83                 .isInstanceOf(DoNotMockException.class)
84                 .hasMessageContaining("Create a real instance instead");
85     }
86 
87     @Test
thrown_exception_includes_special_non_mockable_reason()88     public void thrown_exception_includes_special_non_mockable_reason() {
89         assertThatThrownBy(
90                         () -> {
91                             NotMockableWithReason notMockable = mock(NotMockableWithReason.class);
92                         })
93                 .isInstanceOf(DoNotMockException.class)
94                 .hasMessageContaining("Special reason");
95     }
96 
97     @Test
can_not_mock_class_with_custom_donotmock_annotation()98     public void can_not_mock_class_with_custom_donotmock_annotation() {
99         assertThatThrownBy(
100                         () -> {
101                             NotMockableWithDifferentAnnotation notMockable =
102                                     mock(NotMockableWithDifferentAnnotation.class);
103                         })
104                 .isInstanceOf(DoNotMockException.class);
105     }
106 
107     @DoNotMock
108     private static class NotMockable {}
109 
110     @DoNotMock
111     private interface NotMockableInterface {}
112 
113     @org.mockitousage.annotation.org.mockito.DoNotMock
114     private static class NotMockableWithDifferentAnnotation {}
115 
116     @DoNotMock(reason = "Special reason")
117     private interface NotMockableWithReason {}
118 
119     static class SubclassOfNotMockableInterface implements NotMockableInterface {}
120 
121     private static class DoubleSubclassOfInterface extends SubclassOfNotMockableInterface {}
122 
123     private static class SubclassOfNotMockableSuperclass extends NotMockable {}
124 
125     private interface SubInterfaceOfNotMockableInterface extends NotMockableInterface {}
126 
127     private static class SubclassOfSubInterfaceOfNotMockableInterface
128             implements SubInterfaceOfNotMockableInterface {}
129 
130     private static class TestClass {
131         @Mock private NotMockable notMockable;
132     }
133 }
134