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