Home | History | Annotate | Download | only in basicapi
      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 org.junit.Assert.*;
     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
     28     public void shouldResetOngoingStubbingSoThatMoreMeaningfulExceptionsAreRaised() {
     29         mock.booleanReturningMethod();
     30         reset(mock);
     31         try {
     32             when(null).thenReturn("anything");
     33             fail();
     34         } catch (MissingMethodInvocationException e) {
     35         }
     36     }
     37 
     38     @Test(expected = NotAMockException.class)
     39     public void resettingNonMockIsSafe() {
     40         reset("");
     41     }
     42 
     43     @Test(expected = NotAMockException.class)
     44     public void resettingNullIsSafe() {
     45         reset(new Object[]{null});
     46     }
     47 
     48     @Test
     49     public void shouldRemoveAllStubbing() throws Exception {
     50         when(mock.objectReturningMethod(isA(Integer.class))).thenReturn(100);
     51         when(mock.objectReturningMethod(200)).thenReturn(200);
     52         reset(mock);
     53         assertNull(mock.objectReturningMethod(200));
     54         assertEquals("default behavior should return null", null, mock.objectReturningMethod("blah"));
     55     }
     56 
     57     @Test
     58     public void shouldRemoveAllInteractions() throws Exception {
     59         mock.simpleMethod(1);
     60         reset(mock);
     61         verifyZeroInteractions(mock);
     62     }
     63 
     64     @Test
     65     public void shouldRemoveStubbingToString() throws Exception {
     66         IMethods mockTwo = mock(IMethods.class);
     67         when(mockTwo.toString()).thenReturn("test");
     68         reset(mockTwo);
     69         assertThat(mockTwo.toString()).contains("Mock for IMethods");
     70     }
     71 
     72     @Test
     73     public void shouldStubbingNotBeTreatedAsInteraction() {
     74         when(mock.simpleMethod("one")).thenThrow(new RuntimeException());
     75         doThrow(new RuntimeException()).when(mock).simpleMethod("two");
     76         reset(mock);
     77         verifyZeroInteractions(mock);
     78     }
     79 
     80     @Test
     81     public void shouldNotAffectMockName() {
     82         IMethods mock = mock(IMethods.class, "mockie");
     83         IMethods mockTwo = mock(IMethods.class);
     84         reset(mock);
     85         assertThat(mockTwo.toString()).contains("Mock for IMethods");
     86         assertEquals("mockie", "" + mock);
     87     }
     88 
     89     @Test
     90     public void shouldResetMultipleMocks() {
     91         mock.simpleMethod();
     92         mockTwo.simpleMethod();
     93         reset(mock, mockTwo);
     94         verifyNoMoreInteractions(mock, mockTwo);
     95     }
     96 
     97     @SuppressWarnings({"MockitoUsage", "CheckReturnValue"})
     98     @Test
     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
    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