1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockitousage.basicapi;
7 
8 import org.junit.Test;
9 import org.mockito.Mockito;
10 import org.mockito.exceptions.base.MockitoException;
11 import org.mockito.exceptions.verification.SmartNullPointerException;
12 import org.mockitousage.IMethods;
13 import org.mockitoutil.TestBase;
14 
15 import java.util.LinkedList;
16 import java.util.List;
17 import java.util.Set;
18 
19 import static org.junit.Assert.*;
20 import static org.assertj.core.api.Assertions.assertThat;
21 import static org.mockito.Mockito.*;
22 
23 @SuppressWarnings("unchecked")
24 public class MocksCreationTest extends TestBase {
25 
26     private class HasPrivateConstructor {}
27 
28     @Test
shouldCreateMockWhenConstructorIsPrivate()29     public void shouldCreateMockWhenConstructorIsPrivate() {
30         assertNotNull(Mockito.mock(HasPrivateConstructor.class));
31     }
32 
33     @Test
shouldCombineMockNameAndSmartNulls()34     public void shouldCombineMockNameAndSmartNulls() {
35         //given
36         IMethods mock = mock(IMethods.class, withSettings()
37             .defaultAnswer(RETURNS_SMART_NULLS)
38             .name("great mockie"));
39 
40         //when
41         IMethods smartNull = mock.iMethodsReturningMethod();
42         String name = mock.toString();
43 
44         //then
45         assertThat(name).contains("great mockie");
46         //and
47         try {
48             smartNull.simpleMethod();
49             fail();
50         } catch(SmartNullPointerException e) {}
51     }
52 
53     @Test
shouldCombineMockNameAndExtraInterfaces()54     public void shouldCombineMockNameAndExtraInterfaces() {
55         //given
56         IMethods mock = mock(IMethods.class, withSettings()
57                 .extraInterfaces(List.class)
58                 .name("great mockie"));
59 
60         //when
61         String name = mock.toString();
62 
63         //then
64         assertThat(name).contains("great mockie");
65         //and
66         assertTrue(mock instanceof List);
67     }
68 
69     @Test
shouldSpecifyMockNameViaSettings()70     public void shouldSpecifyMockNameViaSettings() {
71         //given
72         IMethods mock = mock(IMethods.class, withSettings().name("great mockie"));
73 
74         //when
75         String name = mock.toString();
76 
77         //then
78         assertThat(name).contains("great mockie");
79     }
80 
81     @Test
shouldScreamWhenSpyCreatedWithWrongType()82     public void shouldScreamWhenSpyCreatedWithWrongType() {
83         //given
84         List list = new LinkedList();
85         try {
86             //when
87             mock(List.class, withSettings().spiedInstance(list));
88             fail();
89             //then
90         } catch (MockitoException e) {}
91     }
92 
93     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
94     @Test
shouldAllowCreatingSpiesWithCorrectType()95     public void shouldAllowCreatingSpiesWithCorrectType() {
96         List list = new LinkedList();
97         mock(LinkedList.class, withSettings().spiedInstance(list));
98     }
99 
100     @Test
shouldAllowInlineMockCreation()101     public void shouldAllowInlineMockCreation() throws Exception {
102         when(mock(Set.class).isEmpty()).thenReturn(false);
103     }
104 
105 }
106