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
resetState()26     public void resetState() {
27         super.resetState();
28     }
29 
30     @Test(expected=MockitoException.class)
shouldRequireArgumentsWhenVerifyingNoMoreInteractions()31     public void shouldRequireArgumentsWhenVerifyingNoMoreInteractions() {
32         verifyNoMoreInteractions();
33     }
34 
35     @Test(expected=MockitoException.class)
shouldRequireArgumentsWhenVerifyingZeroInteractions()36     public void shouldRequireArgumentsWhenVerifyingZeroInteractions() {
37         verifyZeroInteractions();
38     }
39 
40     @Test(expected=MockitoException.class)
shouldNotCreateInOrderObjectWithoutMocks()41     public void shouldNotCreateInOrderObjectWithoutMocks() {
42         inOrder();
43     }
44 
45     @Test(expected=MockitoException.class)
shouldNotAllowVerifyingInOrderUnfamilarMocks()46     public void shouldNotAllowVerifyingInOrderUnfamilarMocks() {
47         InOrder inOrder = inOrder(mock);
48         inOrder.verify(mockTwo).simpleMethod();
49     }
50 
51     @Test(expected=MissingMethodInvocationException.class)
shouldReportMissingMethodInvocationWhenStubbing()52     public void shouldReportMissingMethodInvocationWhenStubbing() {
53         when(mock.simpleMethod()).thenReturn("this stubbing is required to make sure Stubbable is pulled");
54         when("".toString()).thenReturn("x");
55     }
56 
57     @Test(expected=MockitoException.class)
shouldNotAllowSettingInvalidCheckedException()58     public void shouldNotAllowSettingInvalidCheckedException() throws Exception {
59         when(mock.simpleMethod()).thenThrow(new Exception());
60     }
61 
62     @Test(expected=MockitoException.class)
shouldNotAllowSettingNullThrowable()63     public void shouldNotAllowSettingNullThrowable() throws Exception {
64         when(mock.simpleMethod()).thenThrow(new Throwable[] {null});
65     }
66 
67     @SuppressWarnings("all")
68     @Test(expected=MockitoException.class)
shouldNotAllowSettingNullThrowableVararg()69     public void shouldNotAllowSettingNullThrowableVararg() throws Exception {
70         when(mock.simpleMethod()).thenThrow((Throwable) null);
71     }
72 
73     @Test(expected=MockitoException.class)
shouldNotAllowSettingNullConsecutiveThrowable()74     public void shouldNotAllowSettingNullConsecutiveThrowable() throws Exception {
75         when(mock.simpleMethod()).thenThrow(new RuntimeException(), null);
76     }
77 
78     final class FinalClass {}
79 
80     @Test(expected=MockitoException.class)
shouldNotAllowMockingFinalClassesIfDisabled()81     public void shouldNotAllowMockingFinalClassesIfDisabled() throws Exception {
82         assumeFalse("Inlining mock allows mocking final classes", mock(FinalClass.class).getClass() == FinalClass.class);
83     }
84 
85     @Test(expected=MockitoException.class)
shouldNotAllowMockingPrimitves()86     public void shouldNotAllowMockingPrimitves() throws Exception {
87         mock(Integer.TYPE);
88     }
89 
90     interface ObjectLikeInterface {
equals(Object o)91         boolean equals(Object o);
toString()92         String toString();
hashCode()93         int hashCode();
94     }
95 
96     @Test
shouldNotMockObjectMethodsOnInterface()97     public void shouldNotMockObjectMethodsOnInterface() throws Exception {
98         ObjectLikeInterface inter = mock(ObjectLikeInterface.class);
99 
100         inter.equals(null);
101         inter.toString();
102         inter.hashCode();
103 
104         verifyZeroInteractions(inter);
105     }
106 
shouldNotMockObjectMethodsOnClass()107     public void shouldNotMockObjectMethodsOnClass() throws Exception {
108         Object clazz = mock(ObjectLikeInterface.class);
109 
110         clazz.equals(null);
111         clazz.toString();
112         clazz.hashCode();
113 
114         verifyZeroInteractions(clazz);
115     }
116 }
117