blob: b5892efe3cd5eb510e33bcb32c98d7b238cb8e80 (
plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.basicapi;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.exceptions.verification.SmartNullPointerException;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.RETURNS_SMART_NULLS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
@SuppressWarnings("unchecked")
public class MocksCreationTest extends TestBase {
private class HasPrivateConstructor {}
@Test
public void should_create_mock_when_constructor_is_private() {
assertNotNull(Mockito.mock(HasPrivateConstructor.class));
}
@Test
public void should_combine_mock_name_and_smart_nulls() {
//given
IMethods mock = mock(IMethods.class, withSettings()
.defaultAnswer(RETURNS_SMART_NULLS)
.name("great mockie"));
//when
IMethods smartNull = mock.iMethodsReturningMethod();
String name = mock.toString();
//then
assertThat(name).contains("great mockie");
//and
try {
smartNull.simpleMethod();
fail();
} catch(SmartNullPointerException e) {}
}
@Test
public void should_combine_mock_name_and_extra_interfaces() {
//given
IMethods mock = mock(IMethods.class, withSettings()
.extraInterfaces(List.class)
.name("great mockie"));
//when
String name = mock.toString();
//then
assertThat(name).contains("great mockie");
//and
assertTrue(mock instanceof List);
}
@Test
public void should_specify_mock_name_via_settings() {
//given
IMethods mock = mock(IMethods.class, withSettings().name("great mockie"));
//when
String name = mock.toString();
//then
assertThat(name).contains("great mockie");
}
@Test
public void should_scream_when_spy_created_with_wrong_type() {
//given
List list = new LinkedList();
try {
//when
mock(List.class, withSettings().spiedInstance(list));
fail();
//then
} catch (MockitoException e) {}
}
@SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
@Test
public void should_allow_creating_spies_with_correct_type() {
List list = new LinkedList();
mock(LinkedList.class, withSettings().spiedInstance(list));
}
@Test
public void should_allow_inline_mock_creation() {
when(mock(Set.class).isEmpty()).thenReturn(false);
}
@Retention(RetentionPolicy.RUNTIME)
@interface SomeAnnotation {}
@SomeAnnotation static class Foo {}
@Test
public void should_strip_annotations() {
Foo withAnnotations = mock(Foo.class);
Foo withoutAnnotations = mock(Foo.class, withSettings().withoutAnnotations());
//expect:
assertTrue(withAnnotations.getClass().isAnnotationPresent(SomeAnnotation.class));
assertFalse(withoutAnnotations.getClass().isAnnotationPresent(SomeAnnotation.class));
}
}
|