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.exceptions.misusing.CannotVerifyStubOnlyMock; 11 import org.mockito.exceptions.misusing.MissingMethodInvocationException; 12 import org.mockito.exceptions.verification.NoInteractionsWanted; 13 import org.mockitousage.IMethods; 14 import org.mockitoutil.TestBase; 15 16 import static org.junit.Assert.assertEquals; 17 import static org.junit.Assert.fail; 18 import static org.assertj.core.api.Assertions.assertThat; 19 import static org.mockito.Mockito.*; 20 21 public class BasicStubbingTest extends TestBase { 22 23 private IMethods mock; 24 25 @Before setup()26 public void setup() { 27 mock = mock(IMethods.class); 28 } 29 30 @Test should_evaluate_latest_stubbing_first()31 public void should_evaluate_latest_stubbing_first() throws Exception { 32 when(mock.objectReturningMethod(isA(Integer.class))).thenReturn(100); 33 when(mock.objectReturningMethod(200)).thenReturn(200); 34 35 assertEquals(200, mock.objectReturningMethod(200)); 36 assertEquals(100, mock.objectReturningMethod(666)); 37 assertEquals("default behavior should return null", null, mock.objectReturningMethod("blah")); 38 } 39 40 @Test should_stubbing_be_treated_as_interaction()41 public void should_stubbing_be_treated_as_interaction() throws Exception { 42 when(mock.booleanReturningMethod()).thenReturn(true); 43 44 mock.booleanReturningMethod(); 45 46 try { 47 verifyNoMoreInteractions(mock); 48 fail(); 49 } catch (NoInteractionsWanted e) {} 50 } 51 52 @Test should_allow_stubbing_to_string()53 public void should_allow_stubbing_to_string() throws Exception { 54 IMethods mockTwo = mock(IMethods.class); 55 when(mockTwo.toString()).thenReturn("test"); 56 57 assertThat(mock.toString()).contains("Mock for IMethods"); 58 assertThat(mockTwo.toString()).isEqualTo("test"); 59 } 60 61 @Test should_stubbing_not_be_treated_as_interaction()62 public void should_stubbing_not_be_treated_as_interaction() { 63 when(mock.simpleMethod("one")).thenThrow(new RuntimeException()); 64 doThrow(new RuntimeException()).when(mock).simpleMethod("two"); 65 66 verifyZeroInteractions(mock); 67 } 68 69 @Test unfinished_stubbing_cleans_up_the_state()70 public void unfinished_stubbing_cleans_up_the_state() { 71 reset(mock); 72 try { 73 when("").thenReturn(""); 74 fail(); 75 } catch (MissingMethodInvocationException e) {} 76 77 //anything that can cause state validation 78 verifyZeroInteractions(mock); 79 } 80 81 @Test should_to_string_mock_name()82 public void should_to_string_mock_name() { 83 IMethods mock = mock(IMethods.class, "mockie"); 84 IMethods mockTwo = mock(IMethods.class); 85 86 assertThat(mockTwo.toString()).contains("Mock for IMethods"); 87 assertEquals("mockie", "" + mock); 88 } 89 90 class Foo { toString()91 public final String toString() { 92 return "foo"; 93 } 94 } 95 96 @SuppressWarnings({"CheckReturnValue", "MockitoUsage"}) 97 @Test should_allow_mocking_when_to_string_is_final()98 public void should_allow_mocking_when_to_string_is_final() throws Exception { 99 mock(Foo.class); 100 } 101 102 @Test test_stub_only_not_verifiable()103 public void test_stub_only_not_verifiable() throws Exception { 104 IMethods localMock = mock(IMethods.class, withSettings().stubOnly()); 105 106 when(localMock.objectReturningMethod(isA(Integer.class))).thenReturn(100); 107 when(localMock.objectReturningMethod(200)).thenReturn(200); 108 109 assertEquals(200, localMock.objectReturningMethod(200)); 110 assertEquals(100, localMock.objectReturningMethod(666)); 111 assertEquals("default behavior should return null", null, localMock.objectReturningMethod("blah")); 112 113 try { 114 verify(localMock, atLeastOnce()).objectReturningMethod(eq(200)); 115 fail(); 116 } catch (CannotVerifyStubOnlyMock e) {} 117 } 118 119 @SuppressWarnings("MockitoUsage") 120 @Test test_stub_only_not_verifiable_fail_fast()121 public void test_stub_only_not_verifiable_fail_fast() { 122 IMethods localMock = mock(IMethods.class, withSettings().stubOnly()); 123 124 try { 125 verify(localMock); // throws exception before method invocation 126 fail(); 127 } catch (CannotVerifyStubOnlyMock e) {} 128 } 129 } 130