How to use method of org.mockito.internal.creation.MockSettingsImpl class

Best Mockito code snippet using org.mockito.internal.creation.MockSettingsImpl.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/**
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.creation;
6
7
8import java.util.LinkedList;
9import java.util.List;
10import java.util.Set;
11import org.assertj.core.api.Assertions;
12import org.assertj.core.api.ThrowableAssert;
13import org.junit.Assert;
14import org.junit.Test;
15import org.mockito.Mock;
16import org.mockito.exceptions.base.MockitoException;
17import org.mockito.internal.debugging.VerboseMockInvocationLogger;
18import org.mockito.listeners.InvocationListener;
19import org.mockito.listeners.StubbingLookupListener;
20import org.mockitoutil.TestBase;
21
22
23public class MockSettingsImplTest extends TestBase {
24    private MockSettingsImpl<?> mockSettingsImpl = new MockSettingsImpl<Object>();
25
26    @Mock
27    private InvocationListener invocationListener;
28
29    @Mock
30    private StubbingLookupListener stubbingLookupListener;
31
32    @Test(expected = MockitoException.class)
33    @SuppressWarnings("unchecked")
34    public void shouldNotAllowSettingNullInterface() {
35        mockSettingsImpl.extraInterfaces(List.class, null);
36    }
37
38    @Test(expected = MockitoException.class)
39    @SuppressWarnings("unchecked")
40    public void shouldNotAllowNonInterfaces() {
41        mockSettingsImpl.extraInterfaces(List.class, LinkedList.class);
42    }
43
44    @Test(expected = MockitoException.class)
45    @SuppressWarnings("unchecked")
46    public void shouldNotAllowUsingTheSameInterfaceAsExtra() {
47        mockSettingsImpl.extraInterfaces(List.class, LinkedList.class);
48    }
49
50    @Test(expected = MockitoException.class)
51    @SuppressWarnings("unchecked")
52    public void shouldNotAllowEmptyExtraInterfaces() {
53        mockSettingsImpl.extraInterfaces();
54    }
55
56    @Test(expected = MockitoException.class)
57    @SuppressWarnings("unchecked")
58    public void shouldNotAllowNullArrayOfExtraInterfaces() {
59        mockSettingsImpl.extraInterfaces(((Class<?>[]) (null)));
60    }
61
62    @Test
63    @SuppressWarnings("unchecked")
64    public void shouldAllowMultipleInterfaces() {
65        // when
66        mockSettingsImpl.extraInterfaces(List.class, Set.class);
67        // then
68        Assert.assertEquals(2, mockSettingsImpl.getExtraInterfaces().size());
69        Assert.assertTrue(mockSettingsImpl.getExtraInterfaces().contains(List.class));
70        Assert.assertTrue(mockSettingsImpl.getExtraInterfaces().contains(Set.class));
71    }
72
73    @Test
74    public void shouldSetMockToBeSerializable() throws Exception {
75        // when
76        mockSettingsImpl.serializable();
77        // then
78        Assert.assertTrue(mockSettingsImpl.isSerializable());
79    }
80
81    @Test
82    public void shouldKnowIfIsSerializable() throws Exception {
83        // given
84        Assert.assertFalse(mockSettingsImpl.isSerializable());
85        // when
86        mockSettingsImpl.serializable();
87        // then
88        Assert.assertTrue(mockSettingsImpl.isSerializable());
89    }
90
91    @Test
92    public void shouldAddVerboseLoggingListener() {
93        // given
94        Assert.assertFalse(mockSettingsImpl.hasInvocationListeners());
95        // when
96        mockSettingsImpl.verboseLogging();
97        // then
98        assertThat(mockSettingsImpl.getInvocationListeners()).extracting("class").contains(VerboseMockInvocationLogger.class);
99    }
100
101    @Test
102    public void shouldAddVerboseLoggingListenerOnlyOnce() {
103        // given
104        Assert.assertFalse(mockSettingsImpl.hasInvocationListeners());
105        // when
106        mockSettingsImpl.verboseLogging().verboseLogging();
107        // then
108        Assertions.assertThat(mockSettingsImpl.getInvocationListeners()).hasSize(1);
109    }
110
111    @Test
112    @SuppressWarnings("unchecked")
113    public void shouldAddInvocationListener() {
114        // given
115        Assert.assertFalse(mockSettingsImpl.hasInvocationListeners());
116        // when
117        mockSettingsImpl.invocationListeners(invocationListener);
118        // then
119        Assertions.assertThat(mockSettingsImpl.getInvocationListeners()).contains(invocationListener);
120    }
121
122    @Test
123    @SuppressWarnings("unchecked")
124    public void canAddDuplicateInvocationListeners_ItsNotOurBusinessThere() {
125        // given
126        Assert.assertFalse(mockSettingsImpl.hasInvocationListeners());
127        // when
128        mockSettingsImpl.invocationListeners(invocationListener, invocationListener).invocationListeners(invocationListener);
129        // then
130        Assertions.assertThat(mockSettingsImpl.getInvocationListeners()).containsSequence(invocationListener, invocationListener, invocationListener);
131    }
132
133    @Test
134    public void validates_listeners() {
135        assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
136            public void call() {
137                mockSettingsImpl.addListeners(new Object[]{  }, new LinkedList<Object>(), "myListeners");
138            }
139        }).hasMessageContaining("myListeners() requires at least one listener");
140        assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
141            public void call() {
142                mockSettingsImpl.addListeners(null, new LinkedList<Object>(), "myListeners");
143            }
144        }).hasMessageContaining("myListeners() does not accept null vararg array");
145        assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
146            public void call() {
147                mockSettingsImpl.addListeners(new Object[]{ null }, new LinkedList<Object>(), "myListeners");
148            }
149        }).hasMessageContaining("myListeners() does not accept null listeners");
150    }
151
152    @Test
153    public void validates_stubbing_lookup_listeners() {
154        assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
155            public void call() {
156                mockSettingsImpl.stubbingLookupListeners(new StubbingLookupListener[]{  });
157            }
158        }).hasMessageContaining("stubbingLookupListeners() requires at least one listener");
159        assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
160            public void call() {
161                mockSettingsImpl.stubbingLookupListeners(null);
162            }
163        }).hasMessageContaining("stubbingLookupListeners() does not accept null vararg array");
164        assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
165            public void call() {
166                mockSettingsImpl.stubbingLookupListeners(new StubbingLookupListener[]{ null });
167            }
168        }).hasMessageContaining("stubbingLookupListeners() does not accept null listeners");
169    }
170
171    @Test
172    public void validates_invocation_listeners() {
173        assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
174            public void call() {
175                mockSettingsImpl.invocationListeners(new InvocationListener[]{  });
176            }
177        }).hasMessageContaining("invocationListeners() requires at least one listener");
178        assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
179            public void call() {
180                mockSettingsImpl.invocationListeners(null);
181            }
182        }).hasMessageContaining("invocationListeners() does not accept null vararg array");
183        assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
184            public void call() {
185                mockSettingsImpl.invocationListeners(new InvocationListener[]{ null });
186            }
187        }).hasMessageContaining("invocationListeners() does not accept null listeners");
188    }
189
190    @Test
191    public void addListeners_has_empty_listeners_by_default() {
192        Assert.assertTrue(mockSettingsImpl.getInvocationListeners().isEmpty());
193        Assert.assertTrue(mockSettingsImpl.getStubbingLookupListeners().isEmpty());
194    }
195
196    @Test
197    public void addListeners_shouldAddMockObjectListeners() {
198        // when
199        mockSettingsImpl.invocationListeners(invocationListener);
200        mockSettingsImpl.stubbingLookupListeners(stubbingLookupListener);
201        // then
202        assertThat(mockSettingsImpl.getInvocationListeners()).contains(invocationListener);
203        assertThat(mockSettingsImpl.getStubbingLookupListeners()).contains(stubbingLookupListener);
204    }
205
206    @Test
207    public void addListeners_canAddDuplicateMockObjectListeners_ItsNotOurBusinessThere() {
208        // when
209        mockSettingsImpl.stubbingLookupListeners(stubbingLookupListener).stubbingLookupListeners(stubbingLookupListener).invocationListeners(invocationListener).invocationListeners(invocationListener);
210        // then
211        assertThat(mockSettingsImpl.getInvocationListeners()).containsSequence(invocationListener, invocationListener);
212        assertThat(mockSettingsImpl.getStubbingLookupListeners()).containsSequence(stubbingLookupListener, stubbingLookupListener);
213    }
214}
215
216
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)