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.Test; 9 import org.mockito.Mock; 10 import org.mockito.exceptions.base.MockitoException; 11 import org.mockito.exceptions.misusing.NotAMockException; 12 import org.mockito.exceptions.misusing.NullInsteadOfMockException; 13 import org.mockitousage.IMethods; 14 import org.mockitoutil.TestBase; 15 16 import static org.mockito.Mockito.*; 17 18 public class DescriptiveMessagesOnMisuseTest extends TestBase { 19 20 @Mock private IMethods mock; 21 22 class Foo { finalMethod()23 public final String finalMethod() { 24 return null; 25 } 26 } 27 28 @SuppressWarnings("all") 29 @Test tryDescriptiveMessagesOnMisuse()30 public void tryDescriptiveMessagesOnMisuse() { 31 Foo foo = mock(Foo.class); 32 33 // when(foo.finalMethod()).thenReturn("foo"); 34 // doReturn("foo").when(foo).finalMethod(); 35 // verify(foo).finalMethod(); 36 37 // doReturn("foo"); 38 // doReturn("bar"); 39 40 // verifyNoMoreInteractions(); 41 // verifyNoMoreInteractions(null); 42 // verifyNoMoreInteractions(""); 43 // verifyZeroInteractions(); 44 // verifyZeroInteractions(null); 45 // verifyZeroInteractions(""); 46 // 47 // inOrder(); 48 // inOrder(null); 49 // inOrder("test"); 50 // InOrder inOrder = inOrder(mock(List.class)); 51 // inOrder.verify(mock).simpleMethod(); 52 53 // verify(null); 54 // verify(mock.booleanReturningMethod()); 55 56 // verify(mock).varargs("test", anyString()); 57 58 // when("x").thenReturn("x"); 59 60 // when(mock.simpleMethod()); 61 // when(mock.differentMethod()).thenReturn(""); 62 } 63 64 @Test(expected=NotAMockException.class) shouldScreamWhenWholeMethodPassedToVerify()65 public void shouldScreamWhenWholeMethodPassedToVerify() { 66 verify(mock.booleanReturningMethod()); 67 } 68 69 @Test(expected=NotAMockException.class) shouldScreamWhenWholeMethodPassedToVerifyNoMoreInteractions()70 public void shouldScreamWhenWholeMethodPassedToVerifyNoMoreInteractions() { 71 verifyNoMoreInteractions(mock.byteReturningMethod()); 72 } 73 74 @Test(expected=NotAMockException.class) shouldScreamWhenInOrderCreatedWithDodgyMock()75 public void shouldScreamWhenInOrderCreatedWithDodgyMock() { 76 inOrder("not a mock"); 77 } 78 79 @Test(expected=NullInsteadOfMockException.class) shouldScreamWhenInOrderCreatedWithNulls()80 public void shouldScreamWhenInOrderCreatedWithNulls() { 81 inOrder(mock, null); 82 } 83 84 @Test(expected=NullInsteadOfMockException.class) shouldScreamNullPassedToVerify()85 public void shouldScreamNullPassedToVerify() { 86 verify(null); 87 } 88 89 @Test(expected=NullInsteadOfMockException.class) shouldScreamWhenNotMockPassedToVerifyNoMoreInteractions()90 public void shouldScreamWhenNotMockPassedToVerifyNoMoreInteractions() { 91 verifyNoMoreInteractions(null, "blah"); 92 } 93 94 @SuppressWarnings("all") 95 @Test(expected=MockitoException.class) shouldScreamWhenNullPassedToVerifyNoMoreInteractions()96 public void shouldScreamWhenNullPassedToVerifyNoMoreInteractions() { 97 verifyNoMoreInteractions((Object[])null); 98 } 99 } 100