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