• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.InjectMocks;
10 import org.mockito.Mockito;
11 import org.mockito.MockitoAnnotations;
12 import org.mockito.exceptions.base.MockitoException;
13 import org.mockito.exceptions.verification.SmartNullPointerException;
14 import org.mockito.internal.debugging.LocationImpl;
15 import org.mockitousage.IMethods;
16 import org.mockitoutil.TestBase;
17 
18 import java.util.LinkedList;
19 import java.util.List;
20 import java.util.Set;
21 import java.util.concurrent.TimeUnit;
22 
23 import static junit.framework.TestCase.*;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.mockito.Mockito.*;
26 
27 @SuppressWarnings("unchecked")
28 public class MocksCreationTest extends TestBase {
29 
30     private class HasPrivateConstructor {}
31 
32     @Test
shouldCreateMockWhenConstructorIsPrivate()33     public void shouldCreateMockWhenConstructorIsPrivate() {
34         assertNotNull(Mockito.mock(HasPrivateConstructor.class));
35     }
36 
37     @Test
shouldCombineMockNameAndSmartNulls()38     public void shouldCombineMockNameAndSmartNulls() {
39         //given
40         IMethods mock = mock(IMethods.class, withSettings()
41             .defaultAnswer(RETURNS_SMART_NULLS)
42             .name("great mockie"));
43 
44         //when
45         IMethods smartNull = mock.iMethodsReturningMethod();
46         String name = mock.toString();
47 
48         //then
49         assertThat(name).contains("great mockie");
50         //and
51         try {
52             smartNull.simpleMethod();
53             fail();
54         } catch(SmartNullPointerException e) {}
55     }
56 
57     @Test
shouldCombineMockNameAndExtraInterfaces()58     public void shouldCombineMockNameAndExtraInterfaces() {
59         //given
60         IMethods mock = mock(IMethods.class, withSettings()
61                 .extraInterfaces(List.class)
62                 .name("great mockie"));
63 
64         //when
65         String name = mock.toString();
66 
67         //then
68         assertThat(name).contains("great mockie");
69         //and
70         assertTrue(mock instanceof List);
71     }
72 
73     @Test
shouldSpecifyMockNameViaSettings()74     public void shouldSpecifyMockNameViaSettings() {
75         //given
76         IMethods mock = mock(IMethods.class, withSettings().name("great mockie"));
77 
78         //when
79         String name = mock.toString();
80 
81         //then
82         assertThat(name).contains("great mockie");
83     }
84 
85     @Test
shouldScreamWhenSpyCreatedWithWrongType()86     public void shouldScreamWhenSpyCreatedWithWrongType() {
87         //given
88         List list = new LinkedList();
89         try {
90             //when
91             mock(List.class, withSettings().spiedInstance(list));
92             fail();
93             //then
94         } catch (MockitoException e) {}
95     }
96 
97     @Test
shouldAllowCreatingSpiesWithCorrectType()98     public void shouldAllowCreatingSpiesWithCorrectType() {
99         List list = new LinkedList();
100         mock(LinkedList.class, withSettings().spiedInstance(list));
101     }
102 
103     @Test
shouldAllowInlineMockCreation()104     public void shouldAllowInlineMockCreation() throws Exception {
105         when(mock(Set.class).isEmpty()).thenReturn(false);
106     }
107 
108 }
109