• 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 
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 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 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     @Test
should_allow_mocking_when_to_string_is_final()97     public void should_allow_mocking_when_to_string_is_final() throws Exception {
98         mock(Foo.class);
99     }
100 
101     @Test
test_stub_only_not_verifiable()102     public void test_stub_only_not_verifiable() throws Exception {
103         IMethods localMock = mock(IMethods.class, withSettings().stubOnly());
104 
105         when(localMock.objectReturningMethod(isA(Integer.class))).thenReturn(100);
106         when(localMock.objectReturningMethod(200)).thenReturn(200);
107 
108         assertEquals(200, localMock.objectReturningMethod(200));
109         assertEquals(100, localMock.objectReturningMethod(666));
110         assertEquals("default behavior should return null", null, localMock.objectReturningMethod("blah"));
111 
112         try {
113             verify(localMock, atLeastOnce()).objectReturningMethod(eq(200));
114             fail();
115         } catch (CannotVerifyStubOnlyMock e) {}
116     }
117 }
118