• 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 package org.mockitousage.spies;
6 
7 import static org.assertj.core.api.Assertions.assertThat;
8 import static org.junit.Assert.*;
9 import static org.junit.Assume.assumeTrue;
10 import static org.mockito.Mockito.*;
11 
12 import java.util.Arrays;
13 import java.util.LinkedList;
14 import java.util.List;
15 
16 import org.junit.Test;
17 import org.mockito.InOrder;
18 import org.mockito.Mockito;
19 import org.mockito.exceptions.base.MockitoException;
20 import org.mockito.exceptions.verification.NoInteractionsWanted;
21 import org.mockito.exceptions.verification.TooFewActualInvocations;
22 import org.mockito.exceptions.verification.VerificationInOrderFailure;
23 import org.mockitoutil.TestBase;
24 
25 public class SpyingOnRealObjectsTest extends TestBase {
26 
27     List<String> list = new LinkedList<String>();
28     List<String> spy = Mockito.spy(list);
29 
30     @Test
shouldVerify()31     public void shouldVerify() {
32         spy.add("one");
33         spy.add("two");
34 
35         assertEquals("one", spy.get(0));
36         assertEquals("two", spy.get(1));
37 
38         verify(spy).add("one");
39         verify(spy).add("two");
40     }
41 
42     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
43     @Test
shouldBeAbleToMockObjectBecauseWhyNot()44     public void shouldBeAbleToMockObjectBecauseWhyNot() {
45         spy(new Object());
46     }
47 
48     @Test
shouldStub()49     public void shouldStub() {
50         spy.add("one");
51         when(spy.get(0)).thenReturn("1").thenReturn("1 again");
52 
53         assertEquals("1", spy.get(0));
54         assertEquals("1 again", spy.get(0));
55         assertEquals("one", spy.iterator().next());
56 
57         assertEquals(1, spy.size());
58     }
59 
60     @Test
shouldAllowOverridingStubs()61     public void shouldAllowOverridingStubs() {
62         when(spy.contains(any())).thenReturn(true);
63         when(spy.contains("foo")).thenReturn(false);
64 
65         assertTrue(spy.contains("bar"));
66         assertFalse(spy.contains("foo"));
67     }
68 
69     @Test
shouldStubVoid()70     public void shouldStubVoid() {
71         doNothing().doThrow(new RuntimeException()).when(spy).clear();
72 
73         spy.add("one");
74         spy.clear();
75         try {
76             spy.clear();
77             fail();
78         } catch (RuntimeException e) {
79         }
80 
81         assertEquals(1, spy.size());
82     }
83 
84     @Test
shouldStubWithDoReturnAndVerify()85     public void shouldStubWithDoReturnAndVerify() {
86         doReturn("foo").doReturn("bar").when(spy).get(0);
87 
88         assertEquals("foo", spy.get(0));
89         assertEquals("bar", spy.get(0));
90 
91         verify(spy, times(2)).get(0);
92         verifyNoMoreInteractions(spy);
93     }
94 
95     @Test
shouldVerifyInOrder()96     public void shouldVerifyInOrder() {
97         spy.add("one");
98         spy.add("two");
99 
100         InOrder inOrder = inOrder(spy);
101         inOrder.verify(spy).add("one");
102         inOrder.verify(spy).add("two");
103 
104         verifyNoMoreInteractions(spy);
105     }
106 
107     @Test
shouldVerifyInOrderAndFail()108     public void shouldVerifyInOrderAndFail() {
109         spy.add("one");
110         spy.add("two");
111 
112         InOrder inOrder = inOrder(spy);
113         inOrder.verify(spy).add("two");
114         try {
115             inOrder.verify(spy).add("one");
116             fail();
117         } catch (VerificationInOrderFailure f) {
118         }
119     }
120 
121     @Test
shouldVerifyNumberOfTimes()122     public void shouldVerifyNumberOfTimes() {
123         spy.add("one");
124         spy.add("one");
125 
126         verify(spy, times(2)).add("one");
127         verifyNoMoreInteractions(spy);
128     }
129 
130     @Test
shouldVerifyNumberOfTimesAndFail()131     public void shouldVerifyNumberOfTimesAndFail() {
132         spy.add("one");
133         spy.add("one");
134 
135         try {
136             verify(spy, times(3)).add("one");
137             fail();
138         } catch (TooFewActualInvocations e) {
139         }
140     }
141 
142     @Test
shouldVerifyNoMoreInteractionsAndFail()143     public void shouldVerifyNoMoreInteractionsAndFail() {
144         spy.add("one");
145         spy.add("two");
146 
147         verify(spy).add("one");
148         try {
149             verifyNoMoreInteractions(spy);
150             fail();
151         } catch (NoInteractionsWanted e) {
152         }
153     }
154 
155     @Test
shouldToString()156     public void shouldToString() {
157         spy.add("foo");
158         assertEquals("[foo]", spy.toString());
159     }
160 
161     interface Foo {
print()162         String print();
163     }
164 
165     @Test
shouldAllowSpyingAnonymousClasses()166     public void shouldAllowSpyingAnonymousClasses() {
167         // when
168         Foo spy =
169                 spy(
170                         new Foo() {
171                             public String print() {
172                                 return "foo";
173                             }
174                         });
175 
176         // then
177         assertEquals("foo", spy.print());
178     }
179 
180     @Test
shouldSayNiceMessageWhenSpyingOnPrivateClass()181     public void shouldSayNiceMessageWhenSpyingOnPrivateClass() throws Exception {
182         List<String> real = Arrays.asList("first", "second");
183         try {
184             List<String> spy = spy(real);
185             assumeTrue(
186                     "Using inline mocks, it is possible to spy on private types",
187                     spy.getClass() != real.getClass());
188             fail();
189         } catch (MockitoException e) {
190             assertThat(e)
191                     .hasMessageContaining(
192                             "Most likely it is due to mocking a private class that is not visible to Mockito");
193         }
194     }
195 }
196