Home | History | Annotate | Download | only in bugs
      1 /*
      2  * Copyright (c) 2017 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 package org.mockitousage.bugs;
      6 
      7 import static org.assertj.core.api.Assertions.assertThat;
      8 import static org.mockito.AdditionalMatchers.leq;
      9 import static org.mockito.Matchers.argThat;
     10 import static org.mockito.Matchers.startsWith;
     11 import static org.mockito.Mockito.mock;
     12 import static org.mockito.Mockito.when;
     13 
     14 import java.util.Date;
     15 import org.junit.Rule;
     16 import org.junit.Test;
     17 import org.mockito.ArgumentMatcher;
     18 import org.mockito.Mock;
     19 import org.mockito.junit.MockitoJUnit;
     20 import org.mockito.junit.MockitoRule;
     21 import org.mockitousage.IMethods;
     22 
     23 public class CompareMatcherTest {
     24     private static final Object NOT_A_COMPARABLE = new Object();
     25 
     26     @Rule
     27     public MockitoRule mockitoRule = MockitoJUnit.rule();
     28 
     29     @Mock
     30     public IMethods mock;
     31 
     32     /**
     33      * Should not throw an {@link NullPointerException}
     34      *
     35      * @see <a href="https://github.com/mockito/mockito/issues/457">Bug 457</a>
     36      */
     37     @Test
     38     public void compareNullArgument() {
     39         final IMethods mock = mock(IMethods.class);
     40 
     41         when(mock.forInteger(leq(5))).thenReturn("");
     42 
     43         assertThat(mock.forInteger(null)).isNull();// a default value must be returned
     44     }
     45 
     46     /**
     47      * Should not throw an {@link ClassCastException}
     48      */
     49     @Test
     50     public void compareToNonCompareable() {
     51         when(mock.forObject(leq(5))).thenReturn("");
     52 
     53         assertThat(mock.forObject(NOT_A_COMPARABLE)).isNull();// a default value must be returned
     54     }
     55 
     56     /**
     57      * Should not throw an {@link ClassCastException}
     58      */
     59     @Test
     60     public void compareToNull() {
     61         when(mock.forInteger(leq((Integer) null))).thenReturn("");
     62 
     63         assertThat(mock.forInteger(null)).isNull();// a default value must be returned
     64     }
     65 
     66     /**
     67      * Should not throw an {@link ClassCastException}
     68      */
     69     @Test
     70     public void compareToStringVsInt() {
     71         when(mock.forObject(startsWith("Hello"))).thenReturn("");
     72 
     73         assertThat(mock.forObject(123)).isNull();// a default value must be returned
     74     }
     75 
     76     @Test
     77     public void compareToIntVsString() throws Exception {
     78         when(mock.forObject(leq(5))).thenReturn("");
     79 
     80         mock.forObject("abc");
     81     }
     82 
     83     @Test
     84     public void matchesOverloadsMustBeIgnored() {
     85         class TestMatcher implements ArgumentMatcher<Integer> {
     86             @Override
     87             public boolean matches(Integer arg) {
     88                 return false;
     89             }
     90 
     91             @SuppressWarnings("unused")
     92             public boolean matches(Date arg) {
     93                 throw new UnsupportedOperationException();
     94             }
     95 
     96             @SuppressWarnings("unused")
     97             public boolean matches(Integer arg, Void v) {
     98                 throw new UnsupportedOperationException();
     99             }
    100 
    101         }
    102 
    103         when(mock.forObject(argThat(new TestMatcher()))).thenReturn("x");
    104 
    105         assertThat(mock.forObject(123)).isNull();
    106     }
    107 
    108     @Test
    109     public void matchesWithSubTypeExtendingGenericClass() {
    110         abstract class GenericMatcher<T> implements ArgumentMatcher<T> {}
    111         class TestMatcher extends GenericMatcher<Integer> {
    112             @Override
    113             public boolean matches(Integer argument) {
    114                 return false;
    115             }
    116         }
    117         when(mock.forObject(argThat(new TestMatcher()))).thenReturn("x");
    118 
    119         assertThat(mock.forObject(123)).isNull();
    120     }
    121 
    122     @Test
    123     public void matchesWithSubTypeGenericMethod() {
    124         class GenericMatcher<T> implements ArgumentMatcher<T> {
    125             @Override
    126             public boolean matches(T argument) {
    127                 return false;
    128             }
    129         }
    130         when(mock.forObject(argThat(new GenericMatcher<Integer>()))).thenReturn("x");
    131 
    132         assertThat(mock.forObject(123)).isNull();
    133     }
    134 
    135 }
    136