ReturnsSmartNullsTest.java revision 2637d96c202372854a7c71466ddcc6e90fc4fc53
1/* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5package org.mockito.internal.stubbing.defaultanswers; 6 7import static org.assertj.core.api.Assertions.assertThat; 8 9import org.junit.Test; 10import org.mockito.exceptions.verification.SmartNullPointerException; 11import org.mockito.stubbing.Answer; 12import org.mockitoutil.TestBase; 13 14import static junit.framework.TestCase.assertEquals; 15import static junit.framework.TestCase.fail; 16 17public class ReturnsSmartNullsTest extends TestBase { 18 19 @Test 20 public void should_return_the_usual_default_values_for_primitives() throws Throwable { 21 Answer<Object> answer = new ReturnsSmartNulls(); 22 assertEquals(false , answer.answer(invocationOf(HasPrimitiveMethods.class, "booleanMethod"))); 23 assertEquals((char) 0, answer.answer(invocationOf(HasPrimitiveMethods.class, "charMethod"))); 24 assertEquals((byte) 0, answer.answer(invocationOf(HasPrimitiveMethods.class, "byteMethod"))); 25 assertEquals((short) 0, answer.answer(invocationOf(HasPrimitiveMethods.class, "shortMethod"))); 26 assertEquals(0, answer.answer(invocationOf(HasPrimitiveMethods.class, "intMethod"))); 27 assertEquals(0L, answer.answer(invocationOf(HasPrimitiveMethods.class, "longMethod"))); 28 assertEquals(0f, answer.answer(invocationOf(HasPrimitiveMethods.class, "floatMethod"))); 29 assertEquals(0d, answer.answer(invocationOf(HasPrimitiveMethods.class, "doubleMethod"))); 30 } 31 32 @SuppressWarnings("unused") 33 interface Foo { 34 Foo get(); 35 Foo withArgs(String oneArg, String otherArg); 36 } 37 38 @Test 39 public void should_return_an_object_that_fails_on_any_method_invocation_for_non_primitives() throws Throwable { 40 Answer<Object> answer = new ReturnsSmartNulls(); 41 42 Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "get")); 43 44 try { 45 smartNull.get(); 46 fail(); 47 } catch (SmartNullPointerException expected) {} 48 } 49 50 @Test 51 public void should_return_an_object_that_allows_object_methods() throws Throwable { 52 Answer<Object> answer = new ReturnsSmartNulls(); 53 54 Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "get")); 55 56 assertThat(smartNull.toString()) 57 .contains("SmartNull returned by") 58 .contains("foo.get()"); 59 } 60 61 @Test 62 public void should_print_the_parameters_when_calling_a_method_with_args() throws Throwable { 63 Answer<Object> answer = new ReturnsSmartNulls(); 64 65 Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "withArgs", "oompa", "lumpa")); 66 67 assertThat(smartNull.toString()) 68 .contains("foo.withArgs") 69 .contains("oompa") 70 .contains("lumpa"); 71 } 72 73 @Test 74 public void should_print_the_parameters_on_SmartNullPointerException_message() throws Throwable { 75 Answer<Object> answer = new ReturnsSmartNulls(); 76 77 Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "withArgs", "oompa", "lumpa")); 78 79 try { 80 smartNull.get(); 81 fail(); 82 } catch (SmartNullPointerException e) { 83 assertThat(e) 84 .hasMessageContaining("oompa") 85 .hasMessageContaining("lumpa"); 86 } 87 } 88} 89