• 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 package org.mockitousage.basicapi;
6 
7 import static org.assertj.core.api.Assertions.assertThat;
8 import static org.junit.Assert.assertFalse;
9 import static org.junit.Assert.assertNotNull;
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.RETURNS_SMART_NULLS;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15 import static org.mockito.Mockito.withSettings;
16 
17 import java.lang.annotation.Retention;
18 import java.lang.annotation.RetentionPolicy;
19 import java.util.LinkedList;
20 import java.util.List;
21 import java.util.Set;
22 
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.mockito.exceptions.base.MockitoException;
26 import org.mockito.exceptions.verification.SmartNullPointerException;
27 import org.mockitousage.IMethods;
28 import org.mockitoutil.TestBase;
29 
30 @SuppressWarnings("unchecked")
31 public class MocksCreationTest extends TestBase {
32 
33     private class HasPrivateConstructor {}
34 
35     @Test
should_create_mock_when_constructor_is_private()36     public void should_create_mock_when_constructor_is_private() {
37         assertNotNull(Mockito.mock(HasPrivateConstructor.class));
38     }
39 
40     @Test
should_combine_mock_name_and_smart_nulls()41     public void should_combine_mock_name_and_smart_nulls() {
42         // given
43         IMethods mock =
44                 mock(
45                         IMethods.class,
46                         withSettings().defaultAnswer(RETURNS_SMART_NULLS).name("great mockie"));
47 
48         // when
49         IMethods smartNull = mock.iMethodsReturningMethod();
50         String name = mock.toString();
51 
52         // then
53         assertThat(name).contains("great mockie");
54         // and
55         try {
56             smartNull.simpleMethod();
57             fail();
58         } catch (SmartNullPointerException e) {
59         }
60     }
61 
62     @Test
should_combine_mock_name_and_extra_interfaces()63     public void should_combine_mock_name_and_extra_interfaces() {
64         // given
65         IMethods mock =
66                 mock(
67                         IMethods.class,
68                         withSettings().extraInterfaces(List.class).name("great mockie"));
69 
70         // when
71         String name = mock.toString();
72 
73         // then
74         assertThat(name).contains("great mockie");
75         // and
76         assertTrue(mock instanceof List);
77     }
78 
79     @Test
should_specify_mock_name_via_settings()80     public void should_specify_mock_name_via_settings() {
81         // given
82         IMethods mock = mock(IMethods.class, withSettings().name("great mockie"));
83 
84         // when
85         String name = mock.toString();
86 
87         // then
88         assertThat(name).contains("great mockie");
89     }
90 
91     @Test
should_scream_when_spy_created_with_wrong_type()92     public void should_scream_when_spy_created_with_wrong_type() {
93         // given
94         List list = new LinkedList();
95         try {
96             // when
97             mock(List.class, withSettings().spiedInstance(list));
98             fail();
99             // then
100         } catch (MockitoException e) {
101         }
102     }
103 
104     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
105     @Test
should_allow_creating_spies_with_correct_type()106     public void should_allow_creating_spies_with_correct_type() {
107         List list = new LinkedList();
108         mock(LinkedList.class, withSettings().spiedInstance(list));
109     }
110 
111     @Test
should_allow_inline_mock_creation()112     public void should_allow_inline_mock_creation() {
113         when(mock(Set.class).isEmpty()).thenReturn(false);
114     }
115 
116     @Retention(RetentionPolicy.RUNTIME)
117     @interface SomeAnnotation {}
118 
119     @SomeAnnotation
120     static class Foo {}
121 
122     @Test
should_strip_annotations()123     public void should_strip_annotations() {
124         Foo withAnnotations = mock(Foo.class);
125         Foo withoutAnnotations = mock(Foo.class, withSettings().withoutAnnotations());
126 
127         // expect:
128         assertTrue(withAnnotations.getClass().isAnnotationPresent(SomeAnnotation.class));
129         assertFalse(withoutAnnotations.getClass().isAnnotationPresent(SomeAnnotation.class));
130     }
131 }
132