Home | History | Annotate | Download | only in misuse
      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.misuse;
      7 
      8 import org.junit.After;
      9 import org.junit.Test;
     10 import org.mockito.InOrder;
     11 import org.mockito.Mock;
     12 import org.mockito.exceptions.base.MockitoException;
     13 import org.mockito.exceptions.misusing.MissingMethodInvocationException;
     14 import org.mockitousage.IMethods;
     15 import org.mockitoutil.TestBase;
     16 
     17 import static org.junit.Assume.assumeFalse;
     18 import static org.mockito.Mockito.*;
     19 
     20 public class InvalidUsageTest extends TestBase {
     21 
     22     @Mock private IMethods mock;
     23     @Mock private IMethods mockTwo;
     24 
     25     @After
     26     public void resetState() {
     27         super.resetState();
     28     }
     29 
     30     @Test(expected=MockitoException.class)
     31     public void shouldRequireArgumentsWhenVerifyingNoMoreInteractions() {
     32         verifyNoMoreInteractions();
     33     }
     34 
     35     @Test(expected=MockitoException.class)
     36     public void shouldRequireArgumentsWhenVerifyingZeroInteractions() {
     37         verifyZeroInteractions();
     38     }
     39 
     40     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
     41     @Test(expected=MockitoException.class)
     42     public void shouldNotCreateInOrderObjectWithoutMocks() {
     43         inOrder();
     44     }
     45 
     46     @Test(expected=MockitoException.class)
     47     public void shouldNotAllowVerifyingInOrderUnfamilarMocks() {
     48         InOrder inOrder = inOrder(mock);
     49         inOrder.verify(mockTwo).simpleMethod();
     50     }
     51 
     52     @Test(expected=MissingMethodInvocationException.class)
     53     public void shouldReportMissingMethodInvocationWhenStubbing() {
     54         when(mock.simpleMethod()).thenReturn("this stubbing is required to make sure Stubbable is pulled");
     55         when("".toString()).thenReturn("x");
     56     }
     57 
     58     @Test(expected=MockitoException.class)
     59     public void shouldNotAllowSettingInvalidCheckedException() throws Exception {
     60         when(mock.simpleMethod()).thenThrow(new Exception());
     61     }
     62 
     63     @Test(expected=MockitoException.class)
     64     public void shouldNotAllowSettingNullThrowable() throws Exception {
     65         when(mock.simpleMethod()).thenThrow(new Throwable[] {null});
     66     }
     67 
     68     @SuppressWarnings("all")
     69     @Test(expected=MockitoException.class)
     70     public void shouldNotAllowSettingNullThrowableVararg() throws Exception {
     71         when(mock.simpleMethod()).thenThrow((Throwable) null);
     72     }
     73 
     74     @Test(expected=MockitoException.class)
     75     public void shouldNotAllowSettingNullConsecutiveThrowable() throws Exception {
     76         when(mock.simpleMethod()).thenThrow(new RuntimeException(), null);
     77     }
     78 
     79     final class FinalClass {}
     80 
     81     @Test(expected=MockitoException.class)
     82     public void shouldNotAllowMockingFinalClassesIfDisabled() throws Exception {
     83         assumeFalse("Inlining mock allows mocking final classes", mock(FinalClass.class).getClass() == FinalClass.class);
     84     }
     85 
     86     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
     87     @Test(expected=MockitoException.class)
     88     public void shouldNotAllowMockingPrimitives() throws Exception {
     89         mock(Integer.TYPE);
     90     }
     91 
     92     interface ObjectLikeInterface {
     93         boolean equals(Object o);
     94         String toString();
     95         int hashCode();
     96     }
     97 
     98     @Test
     99     public void shouldNotMockObjectMethodsOnInterface() throws Exception {
    100         ObjectLikeInterface inter = mock(ObjectLikeInterface.class);
    101 
    102         inter.equals(null);
    103         inter.toString();
    104         inter.hashCode();
    105 
    106         verifyZeroInteractions(inter);
    107     }
    108 
    109     @Test
    110     public void shouldNotMockObjectMethodsOnClass() throws Exception {
    111         Object clazz = mock(ObjectLikeInterface.class);
    112 
    113         clazz.equals(null);
    114         clazz.toString();
    115         clazz.hashCode();
    116 
    117         verifyZeroInteractions(clazz);
    118     }
    119 }
    120