Home | History | Annotate | Download | only in mockitousage
      1 package org.mockitousage;
      2 
      3 import org.junit.Before;
      4 import org.junit.Test;
      5 import org.mockito.invocation.InvocationOnMock;
      6 import org.mockito.stubbing.Answer;
      7 
      8 import static org.mockito.BDDMockito.*;
      9 
     10 
     11 public class CompilationWarningsTest {
     12 
     13     @Before
     14     public void pay_attention_to_compilation_warnings_and_JDK_version() {
     15     }
     16 
     17     @Test
     18     public void no_warnings_for_most_common_api() throws Exception {
     19         doReturn(null).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     20         doReturn("a", 12).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     21         doReturn(1000).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     22         doThrow(new NullPointerException()).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     23         doThrow(new NullPointerException(), new IllegalArgumentException()).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     24         doThrow(NullPointerException.class).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     25 
     26         doAnswer(ignore()).doReturn(null).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     27         doAnswer(ignore()).doReturn("a", 12).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     28         doAnswer(ignore()).doReturn(1000).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     29         doAnswer(ignore()).doThrow(new NullPointerException()).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     30         doAnswer(ignore()).doThrow(new NullPointerException(), new IllegalArgumentException()).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     31         doAnswer(ignore()).doThrow(NullPointerException.class).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     32 
     33         when(mock(IMethods.class).objectReturningMethodNoArgs()).thenReturn(null);
     34         when(mock(IMethods.class).objectReturningMethodNoArgs()).thenReturn("a", 12L);
     35         when(mock(IMethods.class).objectReturningMethodNoArgs()).thenReturn(1000);
     36         when(mock(IMethods.class).objectReturningMethodNoArgs()).thenThrow(new NullPointerException());
     37         when(mock(IMethods.class).objectReturningMethodNoArgs()).thenThrow(new NullPointerException(), new IllegalArgumentException());
     38         when(mock(IMethods.class).objectReturningMethodNoArgs()).thenThrow(NullPointerException.class);
     39 
     40         when(mock(IMethods.class).objectReturningMethodNoArgs()).then(ignore()).thenReturn(null);
     41         when(mock(IMethods.class).objectReturningMethodNoArgs()).then(ignore()).thenReturn("a", 12L);
     42         when(mock(IMethods.class).objectReturningMethodNoArgs()).then(ignore()).thenReturn(1000);
     43         when(mock(IMethods.class).objectReturningMethodNoArgs()).then(ignore()).thenThrow(new NullPointerException());
     44         when(mock(IMethods.class).objectReturningMethodNoArgs()).then(ignore()).thenThrow(new NullPointerException(), new IllegalArgumentException());
     45         when(mock(IMethods.class).objectReturningMethodNoArgs()).then(ignore()).thenThrow(NullPointerException.class);
     46 
     47         willReturn(null).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     48         willReturn("a", 12).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     49         willReturn(1000).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     50         willThrow(new NullPointerException()).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     51         willThrow(new NullPointerException(), new IllegalArgumentException()).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     52         willThrow(NullPointerException.class).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     53 
     54         willAnswer(ignore()).willReturn(null).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     55         willAnswer(ignore()).willReturn("a", 12).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     56         willAnswer(ignore()).willReturn(1000).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     57         willAnswer(ignore()).willThrow(new NullPointerException()).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     58         willAnswer(ignore()).willThrow(new NullPointerException(), new IllegalArgumentException()).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     59         willAnswer(ignore()).willThrow(NullPointerException.class).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     60 
     61         given(mock(IMethods.class).objectReturningMethodNoArgs()).willReturn(null);
     62         given(mock(IMethods.class).objectReturningMethodNoArgs()).willReturn("a", 12L);
     63         given(mock(IMethods.class).objectReturningMethodNoArgs()).willReturn(1000);
     64         given(mock(IMethods.class).objectReturningMethodNoArgs()).willThrow(new NullPointerException());
     65         given(mock(IMethods.class).objectReturningMethodNoArgs()).willThrow(new NullPointerException(), new IllegalArgumentException());
     66         given(mock(IMethods.class).objectReturningMethodNoArgs()).willThrow(NullPointerException.class);
     67 
     68         given(mock(IMethods.class).objectReturningMethodNoArgs()).will(ignore()).willReturn(null);
     69         given(mock(IMethods.class).objectReturningMethodNoArgs()).will(ignore()).willReturn("a", 12L);
     70         given(mock(IMethods.class).objectReturningMethodNoArgs()).will(ignore()).willReturn(1000);
     71         given(mock(IMethods.class).objectReturningMethodNoArgs()).will(ignore()).willThrow(new NullPointerException());
     72         given(mock(IMethods.class).objectReturningMethodNoArgs()).will(ignore()).willThrow(new NullPointerException(), new IllegalArgumentException());
     73         given(mock(IMethods.class).objectReturningMethodNoArgs()).will(ignore()).willThrow(NullPointerException.class);
     74     }
     75 
     76     @Test
     77     @SuppressWarnings("unchecked")
     78     public void heap_pollution_JDK7plus_warning_avoided_BUT_now_unchecked_generic_array_creation_warnings_ON_JDK5plus_environment() throws Exception {
     79         doThrow(NullPointerException.class, IllegalArgumentException.class).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     80         when(mock(IMethods.class).objectReturningMethodNoArgs()).thenThrow(NullPointerException.class, IllegalArgumentException.class);
     81         doAnswer(ignore()).doThrow(NullPointerException.class, IllegalArgumentException.class).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     82 
     83         willThrow(NullPointerException.class, IllegalArgumentException.class).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     84         given(mock(IMethods.class).objectReturningMethodNoArgs()).willThrow(NullPointerException.class, IllegalArgumentException.class);
     85         willAnswer(ignore()).willThrow(NullPointerException.class, IllegalArgumentException.class).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     86     }
     87 
     88     @Test
     89     public void unchecked_confusing_null_argument_warnings() throws Exception {
     90         doReturn(null, (Object[]) null).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     91         doAnswer(ignore()).doReturn(null, (Object[]) null).when(mock(IMethods.class)).objectReturningMethodNoArgs();
     92         when(mock(IMethods.class).objectReturningMethodNoArgs()).thenReturn(null, (Object[]) null);
     93         when(mock(IMethods.class).objectReturningMethodNoArgs()).then(ignore()).thenReturn(null, (Object[]) null);
     94         willReturn(null, (Object[]) null).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     95         given(mock(IMethods.class).objectReturningMethodNoArgs()).willReturn(null, (Object[]) null);
     96         willAnswer(ignore()).willReturn(null, (Object[]) null).given(mock(IMethods.class)).objectReturningMethodNoArgs();
     97         given(mock(IMethods.class).objectReturningMethodNoArgs()).will(ignore()).willReturn(null, (Object[]) null);
     98     }
     99 
    100     private static Answer<?> ignore() {
    101         return new Answer<Object>() {
    102             @Override
    103             public Object answer(InvocationOnMock invocation) throws Throwable {
    104                 return null;
    105             }
    106         };
    107     }
    108 }
    109