/
DoNotMockTest.java
73 lines (56 loc) · 2.17 KB
/
DoNotMockTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Copyright (c) 2019 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.annotation;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import org.junit.Test;
import org.mockito.DoNotMock;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.exceptions.misusing.DoNotMockException;
public class DoNotMockTest {
@Test
public void can_not_mock_class_annotated_with_donotmock() {
assertThatThrownBy(() -> {
NotMockable notMockable = mock(NotMockable.class);
}).isInstanceOf(DoNotMockException.class);
}
@Test
public void can_not_mock_class_via_mock_annotation() {
assertThatThrownBy(() -> {
MockitoAnnotations.initMocks(new TestClass());
}).isInstanceOf(DoNotMockException.class);
}
@Test
public void can_not_mock_class_with_interface_annotated_with_donotmock() {
assertThatThrownBy(() -> {
SubclassOfNotMockableInterface notMockable = mock(SubclassOfNotMockableInterface.class);
}).isInstanceOf(DoNotMockException.class);
}
@Test
public void can_not_mock_subclass_with_unmockable_interface() {
assertThatThrownBy(() -> {
DoubleSubclassOfInterface notMockable = mock(DoubleSubclassOfInterface.class);
}).isInstanceOf(DoNotMockException.class);
}
@Test
public void can_not_mock_subclass_with_unmockable_superclass() {
assertThatThrownBy(() -> {
SubclassOfNotMockableSuperclass notMockable = mock(SubclassOfNotMockableSuperclass.class);
}).isInstanceOf(DoNotMockException.class);
}
@DoNotMock
private static class NotMockable {
}
@DoNotMock
private interface NotMockableInterface {
}
static class SubclassOfNotMockableInterface implements NotMockableInterface {}
private static class DoubleSubclassOfInterface extends SubclassOfNotMockableInterface {}
private static class SubclassOfNotMockableSuperclass extends NotMockable {}
private static class TestClass {
@Mock private NotMockable notMockable;
}
}