• 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.basicapi;
6 
7 import org.junit.Test;
8 import org.mockito.Mock;
9 import org.mockito.exceptions.misusing.MissingMethodInvocationException;
10 import org.mockito.exceptions.misusing.NotAMockException;
11 import org.mockito.exceptions.misusing.UnfinishedVerificationException;
12 import org.mockitousage.IMethods;
13 import org.mockitoutil.TestBase;
14 
15 import static junit.framework.TestCase.*;
16 import static org.assertj.core.api.Assertions.assertThat;
17 import static org.mockito.Mockito.*;
18 
19 public class ResetTest extends TestBase {
20 
21     @Mock
22     private IMethods mock;
23 
24     @Mock
25     private IMethods mockTwo;
26 
27     @Test
shouldResetOngoingStubbingSoThatMoreMeaningfulExceptionsAreRaised()28     public void shouldResetOngoingStubbingSoThatMoreMeaningfulExceptionsAreRaised() {
29         mock(IMethods.class);
30         mock.booleanReturningMethod();
31         reset(mock);
32         try {
33             when(null).thenReturn("anything");
34             fail();
35         } catch (MissingMethodInvocationException e) {
36         }
37     }
38 
39     @Test(expected = NotAMockException.class)
resettingNonMockIsSafe()40     public void resettingNonMockIsSafe() {
41         reset("");
42     }
43 
44     @Test(expected = NotAMockException.class)
resettingNullIsSafe()45     public void resettingNullIsSafe() {
46         reset(new Object[]{null});
47     }
48 
49     @Test
shouldRemoveAllStubbing()50     public void shouldRemoveAllStubbing() throws Exception {
51         when(mock.objectReturningMethod(isA(Integer.class))).thenReturn(100);
52         when(mock.objectReturningMethod(200)).thenReturn(200);
53         reset(mock);
54         assertNull(mock.objectReturningMethod(200));
55         assertEquals("default behavior should return null", null, mock.objectReturningMethod("blah"));
56     }
57 
58     @Test
shouldRemoveAllInteractions()59     public void shouldRemoveAllInteractions() throws Exception {
60         mock.simpleMethod(1);
61         reset(mock);
62         verifyZeroInteractions(mock);
63     }
64 
65     @Test
shouldRemoveStubbingToString()66     public void shouldRemoveStubbingToString() throws Exception {
67         IMethods mockTwo = mock(IMethods.class);
68         when(mockTwo.toString()).thenReturn("test");
69         reset(mockTwo);
70         assertThat(mockTwo.toString()).contains("Mock for IMethods");
71     }
72 
73     @Test
shouldStubbingNotBeTreatedAsInteraction()74     public void shouldStubbingNotBeTreatedAsInteraction() {
75         when(mock.simpleMethod("one")).thenThrow(new RuntimeException());
76         doThrow(new RuntimeException()).when(mock).simpleMethod("two");
77         reset(mock);
78         verifyZeroInteractions(mock);
79     }
80 
81     @Test
shouldNotAffectMockName()82     public void shouldNotAffectMockName() {
83         IMethods mock = mock(IMethods.class, "mockie");
84         IMethods mockTwo = mock(IMethods.class);
85         reset(mock);
86         assertThat(mockTwo.toString()).contains("Mock for IMethods");
87         assertEquals("mockie", "" + mock);
88     }
89 
90     @Test
shouldResetMultipleMocks()91     public void shouldResetMultipleMocks() {
92         mock.simpleMethod();
93         mockTwo.simpleMethod();
94         reset(mock, mockTwo);
95         verifyNoMoreInteractions(mock, mockTwo);
96     }
97 
98     @Test
shouldValidateStateWhenResetting()99     public void shouldValidateStateWhenResetting() {
100         //invalid verify:
101         verify(mock);
102 
103         try {
104             reset(mockTwo);
105             fail();
106         } catch (UnfinishedVerificationException e) {
107         }
108     }
109 
110     @Test
shouldMaintainPreviousDefaultAnswer()111     public void shouldMaintainPreviousDefaultAnswer() {
112         //given
113         mock = mock(IMethods.class, RETURNS_MOCKS);
114         //when
115         reset(mock);
116         //then
117         assertNotNull(mock.iMethodsReturningMethod());
118     }
119 }
120