• Home
  • History
  • Annotate
  • 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.stubbing;
7 
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.mockito.Mockito;
11 import org.mockito.exceptions.verification.SmartNullPointerException;
12 import org.mockito.exceptions.verification.WantedButNotInvoked;
13 import org.mockitousage.IMethods;
14 import org.mockitoutil.TestBase;
15 
16 import static junit.framework.TestCase.assertEquals;
17 import static junit.framework.TestCase.fail;
18 import static org.assertj.core.api.Assertions.assertThat;
19 import static org.mockito.Mockito.*;
20 
21 public class SmartNullsStubbingTest extends TestBase {
22 
23     private IMethods mock;
24 
25     @Before
setup()26     public void setup() {
27         mock = mock(IMethods.class, Mockito.RETURNS_SMART_NULLS);
28     }
29 
unstubbedMethodInvokedHere(IMethods mock)30     public IMethods unstubbedMethodInvokedHere(IMethods mock) {
31         return mock.iMethodsReturningMethod();
32     }
33 
34     @Test
shouldSmartNPEPointToUnstubbedCall()35     public void shouldSmartNPEPointToUnstubbedCall() throws Exception {
36         IMethods methods = unstubbedMethodInvokedHere(mock);
37         try {
38             methods.simpleMethod();
39             fail();
40         } catch (SmartNullPointerException e) {
41             assertThat(e).hasMessageContaining("unstubbedMethodInvokedHere(");
42         }
43     }
44 
45     interface Bar {
boo()46         void boo();
47     }
48 
49     class Foo {
getSomeClass()50         Foo getSomeClass() {
51             return null;
52         }
53 
getSomeInterface()54         Bar getSomeInterface() {
55             return null;
56         }
57 
getBarWithParams(int x, String y)58         Bar getBarWithParams(int x, String y) {
59             return null;
60         }
61 
boo()62         void boo() {}
63     }
64 
65     @Test
shouldThrowSmartNPEWhenMethodReturnsClass()66     public void shouldThrowSmartNPEWhenMethodReturnsClass() throws Exception {
67         Foo mock = mock(Foo.class, RETURNS_SMART_NULLS);
68         Foo foo = mock.getSomeClass();
69         try {
70             foo.boo();
71             fail();
72         } catch (SmartNullPointerException e) {}
73     }
74 
75     @Test
shouldThrowSmartNPEWhenMethodReturnsInterface()76     public void shouldThrowSmartNPEWhenMethodReturnsInterface() throws Exception {
77         Foo mock = mock(Foo.class, RETURNS_SMART_NULLS);
78         Bar bar = mock.getSomeInterface();
79         try {
80             bar.boo();
81             fail();
82         } catch (SmartNullPointerException e) {}
83     }
84 
85 
86     @Test
shouldReturnOrdinaryEmptyValuesForOrdinaryTypes()87     public void shouldReturnOrdinaryEmptyValuesForOrdinaryTypes() throws Exception {
88         IMethods mock = mock(IMethods.class, RETURNS_SMART_NULLS);
89 
90         assertEquals("", mock.stringReturningMethod());
91         assertEquals(0, mock.intReturningMethod());
92         assertEquals(true, mock.listReturningMethod().isEmpty());
93         assertEquals(0, mock.arrayReturningMethod().length);
94     }
95 
96     @Test
shouldNotThrowSmartNullPointerOnToString()97     public void shouldNotThrowSmartNullPointerOnToString() {
98         Object smartNull = mock.objectReturningMethod();
99         try {
100             verify(mock).simpleMethod(smartNull);
101             fail();
102         } catch (WantedButNotInvoked e) {}
103     }
104 
105     @Test
shouldNotThrowSmartNullPointerOnObjectMethods()106     public void shouldNotThrowSmartNullPointerOnObjectMethods() {
107         Object smartNull = mock.objectReturningMethod();
108         smartNull.toString();
109     }
110 
111     @Test
shouldShowParameters()112     public void shouldShowParameters() {
113         Foo foo = mock(Foo.class, RETURNS_SMART_NULLS);
114         Bar smartNull = foo.getBarWithParams(10, "yes sir");
115 
116         try {
117             smartNull.boo();
118             fail();
119         } catch (Exception e) {
120             assertThat(e).hasMessageContaining("yes sir");
121         }
122     }
123 
124     @Test
shouldShowParametersWhenParamsAreHuge()125     public void shouldShowParametersWhenParamsAreHuge() {
126         Foo foo = mock(Foo.class, RETURNS_SMART_NULLS);
127         String longStr = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
128         Bar smartNull = foo.getBarWithParams(10, longStr);
129 
130         try {
131             smartNull.boo();
132             fail();
133         } catch (Exception e) {
134             assertThat(e).hasMessageContaining("Lorem Ipsum");
135         }
136     }
137 }
138