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.stubbing; 6 7 import static org.assertj.core.api.Assertions.assertThat; 8 import static org.junit.Assert.assertEquals; 9 import static org.junit.Assert.fail; 10 import static org.mockito.Mockito.*; 11 12 import org.junit.Before; 13 import org.junit.Test; 14 import org.mockito.Mockito; 15 import org.mockito.exceptions.verification.SmartNullPointerException; 16 import org.mockito.exceptions.verification.WantedButNotInvoked; 17 import org.mockitousage.IMethods; 18 import org.mockitoutil.TestBase; 19 20 public class SmartNullsStubbingTest extends TestBase { 21 22 private IMethods mock; 23 24 @Before setup()25 public void setup() { 26 mock = mock(IMethods.class, Mockito.RETURNS_SMART_NULLS); 27 } 28 unstubbedMethodInvokedHere(IMethods mock)29 public IMethods unstubbedMethodInvokedHere(IMethods mock) { 30 return mock.iMethodsReturningMethod(); 31 } 32 33 @Test shouldSmartNPEPointToUnstubbedCall()34 public void shouldSmartNPEPointToUnstubbedCall() throws Exception { 35 IMethods methods = unstubbedMethodInvokedHere(mock); 36 try { 37 methods.simpleMethod(); 38 fail(); 39 } catch (SmartNullPointerException e) { 40 assertThat(e).hasMessageContaining("unstubbedMethodInvokedHere("); 41 } 42 } 43 44 @Test should_not_throw_NPE_when_verifying_with_returns_smart_nulls()45 public void should_not_throw_NPE_when_verifying_with_returns_smart_nulls() { 46 Foo mock = mock(Foo.class, RETURNS_SMART_NULLS); 47 48 when(mock.returnsFromArg(null)).thenReturn("Does not fail."); 49 50 assertThat((Object) mock.returnsFromArg(null)).isEqualTo("Does not fail."); 51 } 52 53 interface Bar { boo()54 void boo(); 55 } 56 57 class Foo { getSomeClass()58 Foo getSomeClass() { 59 return null; 60 } 61 getSomeInterface()62 Bar getSomeInterface() { 63 return null; 64 } 65 getBarWithParams(int x, String y)66 Bar getBarWithParams(int x, String y) { 67 return null; 68 } 69 returnsFromArg(T arg)70 <T> T returnsFromArg(T arg) { 71 return arg; 72 } 73 boo()74 void boo() {} 75 } 76 77 @Test shouldThrowSmartNPEWhenMethodReturnsClass()78 public void shouldThrowSmartNPEWhenMethodReturnsClass() throws Exception { 79 Foo mock = mock(Foo.class, RETURNS_SMART_NULLS); 80 Foo foo = mock.getSomeClass(); 81 try { 82 foo.boo(); 83 fail(); 84 } catch (SmartNullPointerException e) { 85 } 86 } 87 88 @Test shouldThrowSmartNPEWhenMethodReturnsInterface()89 public void shouldThrowSmartNPEWhenMethodReturnsInterface() throws Exception { 90 Foo mock = mock(Foo.class, RETURNS_SMART_NULLS); 91 Bar bar = mock.getSomeInterface(); 92 try { 93 bar.boo(); 94 fail(); 95 } catch (SmartNullPointerException e) { 96 } 97 } 98 99 @Test shouldReturnOrdinaryEmptyValuesForOrdinaryTypes()100 public void shouldReturnOrdinaryEmptyValuesForOrdinaryTypes() throws Exception { 101 IMethods mock = mock(IMethods.class, RETURNS_SMART_NULLS); 102 103 assertEquals("", mock.stringReturningMethod()); 104 assertEquals(0, mock.intReturningMethod()); 105 assertEquals(true, mock.listReturningMethod().isEmpty()); 106 assertEquals(0, mock.arrayReturningMethod().length); 107 } 108 109 @Test shouldNotThrowSmartNullPointerOnToString()110 public void shouldNotThrowSmartNullPointerOnToString() { 111 Object smartNull = mock.objectReturningMethod(); 112 try { 113 verify(mock).simpleMethod(smartNull); 114 fail(); 115 } catch (WantedButNotInvoked e) { 116 } 117 } 118 119 @Test shouldNotThrowSmartNullPointerOnObjectMethods()120 public void shouldNotThrowSmartNullPointerOnObjectMethods() { 121 Object smartNull = mock.objectReturningMethod(); 122 String ignored = smartNull.toString(); 123 } 124 125 @Test shouldShowParameters()126 public void shouldShowParameters() { 127 Foo foo = mock(Foo.class, RETURNS_SMART_NULLS); 128 Bar smartNull = foo.getBarWithParams(10, "yes sir"); 129 130 try { 131 smartNull.boo(); 132 fail(); 133 } catch (Exception e) { 134 assertThat(e).hasMessageContaining("yes sir"); 135 } 136 } 137 138 @Test shouldShowParametersWhenParamsAreHuge()139 public void shouldShowParametersWhenParamsAreHuge() { 140 Foo foo = mock(Foo.class, RETURNS_SMART_NULLS); 141 String longStr = 142 "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."; 143 Bar smartNull = foo.getBarWithParams(10, longStr); 144 145 try { 146 smartNull.boo(); 147 fail(); 148 } catch (Exception e) { 149 assertThat(e).hasMessageContaining("Lorem Ipsum"); 150 } 151 } 152 } 153