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.annotation; 6 7 import org.assertj.core.api.Assertions; 8 import org.junit.Test; 9 import org.mockito.*; 10 import org.mockito.exceptions.base.MockitoException; 11 import org.mockitoutil.TestBase; 12 13 import java.util.List; 14 15 import static junit.framework.TestCase.fail; 16 17 public class WrongSetOfAnnotationsTest extends TestBase { 18 19 @Test(expected=MockitoException.class) should_not_allow_Mock_and_Spy()20 public void should_not_allow_Mock_and_Spy() throws Exception { 21 MockitoAnnotations.initMocks(new Object() { 22 @Mock @Spy List<?> mock; 23 }); 24 } 25 26 @Test should_not_allow_Spy_and_InjectMocks_on_interfaces()27 public void should_not_allow_Spy_and_InjectMocks_on_interfaces() throws Exception { 28 try { 29 MockitoAnnotations.initMocks(new Object() { @InjectMocks @Spy List<?> mock; }); 30 fail(); 31 } catch (MockitoException me) { 32 Assertions.assertThat(me.getMessage()).contains("'List' is an interface"); 33 } 34 } 35 36 @Test should_allow_Spy_and_InjectMocks()37 public void should_allow_Spy_and_InjectMocks() throws Exception { 38 MockitoAnnotations.initMocks(new Object() { 39 @InjectMocks 40 @Spy 41 WithDependency mock; 42 }); 43 } 44 static class WithDependency { List<?> list; } 45 46 @Test(expected=MockitoException.class) should_not_allow_Mock_and_InjectMocks()47 public void should_not_allow_Mock_and_InjectMocks() throws Exception { 48 MockitoAnnotations.initMocks(new Object() { 49 @InjectMocks @Mock List<?> mock; 50 }); 51 } 52 53 @Test(expected=MockitoException.class) should_not_allow_Captor_and_Mock()54 public void should_not_allow_Captor_and_Mock() throws Exception { 55 MockitoAnnotations.initMocks(new Object() { 56 @Mock @Captor ArgumentCaptor<?> captor; 57 }); 58 } 59 60 @Test(expected=MockitoException.class) should_not_allow_Captor_and_Spy()61 public void should_not_allow_Captor_and_Spy() throws Exception { 62 MockitoAnnotations.initMocks(new Object() { 63 @Spy @Captor ArgumentCaptor<?> captor; 64 }); 65 } 66 67 @Test(expected=MockitoException.class) should_not_allow_Captor_and_InjectMocks()68 public void should_not_allow_Captor_and_InjectMocks() throws Exception { 69 MockitoAnnotations.initMocks(new Object() { 70 @InjectMocks @Captor ArgumentCaptor<?> captor; 71 }); 72 } 73 } 74