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