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 static org.assertj.core.api.Assertions.assertThat; 8 import static org.junit.Assert.*; 9 10 import java.lang.annotation.Retention; 11 import java.lang.annotation.RetentionPolicy; 12 import java.util.ArrayList; 13 import java.util.List; 14 import java.util.Set; 15 16 import org.junit.Test; 17 import org.mockito.*; 18 import org.mockito.exceptions.base.MockitoException; 19 import org.mockitousage.IMethods; 20 import org.mockitoutil.TestBase; 21 22 public class CaptorAnnotationTest extends TestBase { 23 24 @Retention(RetentionPolicy.RUNTIME) 25 public @interface NotAMock {} 26 27 @Captor final ArgumentCaptor<String> finalCaptor = ArgumentCaptor.forClass(String.class); 28 29 @Captor ArgumentCaptor<List<List<String>>> genericsCaptor; 30 31 @SuppressWarnings("rawtypes") 32 @Captor 33 ArgumentCaptor nonGenericCaptorIsAllowed; 34 35 @Mock MockInterface mockInterface; 36 37 @NotAMock Set<?> notAMock; 38 39 public interface MockInterface { testMe(String simple, List<List<String>> genericList)40 void testMe(String simple, List<List<String>> genericList); 41 } 42 43 @Test testNormalUsage()44 public void testNormalUsage() { 45 46 MockitoAnnotations.openMocks(this); 47 48 // check if assigned correctly 49 assertNotNull(finalCaptor); 50 assertNotNull(genericsCaptor); 51 assertNotNull(nonGenericCaptorIsAllowed); 52 assertNull(notAMock); 53 54 // use captors in the field to be sure they are cool 55 String argForFinalCaptor = "Hello"; 56 ArrayList<List<String>> argForGenericsCaptor = new ArrayList<List<String>>(); 57 58 mockInterface.testMe(argForFinalCaptor, argForGenericsCaptor); 59 60 Mockito.verify(mockInterface).testMe(finalCaptor.capture(), genericsCaptor.capture()); 61 62 assertEquals(argForFinalCaptor, finalCaptor.getValue()); 63 assertEquals(argForGenericsCaptor, genericsCaptor.getValue()); 64 } 65 66 public static class WrongType { 67 @Captor List<?> wrongType; 68 } 69 70 @Test shouldScreamWhenWrongTypeForCaptor()71 public void shouldScreamWhenWrongTypeForCaptor() { 72 try { 73 MockitoAnnotations.openMocks(new WrongType()); 74 fail(); 75 } catch (MockitoException e) { 76 } 77 } 78 79 public static class ToManyAnnotations { 80 @Captor @Mock ArgumentCaptor<List> missingGenericsField; 81 } 82 83 @Test shouldScreamWhenMoreThanOneMockitoAnnotation()84 public void shouldScreamWhenMoreThanOneMockitoAnnotation() { 85 try { 86 MockitoAnnotations.openMocks(new ToManyAnnotations()); 87 fail(); 88 } catch (MockitoException e) { 89 assertThat(e) 90 .hasMessageContaining("missingGenericsField") 91 .hasMessageContaining("multiple Mockito annotations"); 92 } 93 } 94 95 @Test shouldScreamWhenInitializingCaptorsForNullClass()96 public void shouldScreamWhenInitializingCaptorsForNullClass() throws Exception { 97 try { 98 MockitoAnnotations.openMocks(null); 99 fail(); 100 } catch (MockitoException e) { 101 } 102 } 103 104 @Test shouldLookForAnnotatedCaptorsInSuperClasses()105 public void shouldLookForAnnotatedCaptorsInSuperClasses() throws Exception { 106 Sub sub = new Sub(); 107 MockitoAnnotations.openMocks(sub); 108 109 assertNotNull(sub.getCaptor()); 110 assertNotNull(sub.getBaseCaptor()); 111 assertNotNull(sub.getSuperBaseCaptor()); 112 } 113 114 class SuperBase { 115 @Captor private ArgumentCaptor<IMethods> mock; 116 getSuperBaseCaptor()117 public ArgumentCaptor<IMethods> getSuperBaseCaptor() { 118 return mock; 119 } 120 } 121 122 class Base extends SuperBase { 123 @Captor private ArgumentCaptor<IMethods> mock; 124 getBaseCaptor()125 public ArgumentCaptor<IMethods> getBaseCaptor() { 126 return mock; 127 } 128 } 129 130 class Sub extends Base { 131 @Captor private ArgumentCaptor<IMethods> mock; 132 getCaptor()133 public ArgumentCaptor<IMethods> getCaptor() { 134 return mock; 135 } 136 } 137 } 138