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.matchers; 6 7 import static org.assertj.core.api.Assertions.assertThat; 8 import static org.hamcrest.CoreMatchers.is; 9 import static org.junit.Assert.*; 10 import static org.mockito.Mockito.verify; 11 import static org.mockito.Mockito.when; 12 import static org.mockito.hamcrest.MockitoHamcrest.*; 13 14 import org.hamcrest.BaseMatcher; 15 import org.hamcrest.Description; 16 import org.junit.Test; 17 import org.mockito.ArgumentMatcher; 18 import org.mockito.Mock; 19 import org.mockito.Mockito; 20 import org.mockito.exceptions.verification.opentest4j.ArgumentsAreDifferent; 21 import org.mockitousage.IMethods; 22 import org.mockitoutil.TestBase; 23 24 public class HamcrestMatchersTest extends TestBase { 25 26 private final class ContainsX extends BaseMatcher<String> { matches(Object o)27 public boolean matches(Object o) { 28 return ((String) o).contains("X"); 29 } 30 describeTo(Description d)31 public void describeTo(Description d) { 32 d.appendText("contains 'X'"); 33 } 34 } 35 36 @Mock private IMethods mock; 37 38 @Test stubs_with_hamcrest_matcher()39 public void stubs_with_hamcrest_matcher() { 40 when(mock.simpleMethod(argThat(new ContainsX()))).thenReturn("X"); 41 assertNull(mock.simpleMethod("blah")); 42 assertEquals("X", mock.simpleMethod("blah X blah")); 43 } 44 45 @Test verifies_with_hamcrest_matcher()46 public void verifies_with_hamcrest_matcher() { 47 mock.simpleMethod("blah"); 48 49 try { 50 verify(mock).simpleMethod(argThat(new ContainsX())); 51 fail(); 52 } catch (ArgumentsAreDifferent e) { 53 assertThat(e).hasMessageContaining("contains 'X'"); 54 } 55 } 56 57 private class IntMatcher extends BaseMatcher<Integer> { matches(Object o)58 public boolean matches(Object o) { 59 return true; 60 } 61 describeTo(Description description)62 public void describeTo(Description description) {} 63 } 64 65 @Test supports_primitive_matchers()66 public void supports_primitive_matchers() { 67 when(mock.intArgumentReturningInt(argThat(new IntMatcher()))).thenReturn(5); 68 assertEquals(5, mock.intArgumentReturningInt(10)); 69 } 70 71 @Test supports_primitive_matchers_from_core_library()72 public void supports_primitive_matchers_from_core_library() { 73 mock.oneArg(true); 74 mock.oneArg((byte) 1); 75 mock.oneArg(2); 76 mock.oneArg(3L); 77 mock.oneArg('4'); 78 mock.oneArg(5.0D); 79 mock.oneArg(6.0F); 80 81 verify(mock).oneArg(booleanThat(is(true))); 82 verify(mock).oneArg(byteThat(is((byte) 1))); 83 verify(mock).oneArg(intThat(is(2))); 84 verify(mock).oneArg(longThat(is(3L))); 85 verify(mock).oneArg(charThat(is('4'))); 86 verify(mock).oneArg(doubleThat(is(5.0D))); 87 verify(mock).oneArg(floatThat(is(6.0F))); 88 } 89 90 @SuppressWarnings("rawtypes") 91 private class NonGenericMatcher extends BaseMatcher { matches(Object o)92 public boolean matches(Object o) { 93 return true; 94 } 95 describeTo(Description description)96 public void describeTo(Description description) {} 97 } 98 99 @Test supports_non_generic_matchers()100 public void supports_non_generic_matchers() { 101 when(mock.intArgumentReturningInt(nonGenericMatcher())).thenReturn(5); 102 assertEquals(5, mock.intArgumentReturningInt(10)); 103 } 104 105 @SuppressWarnings("unchecked") nonGenericMatcher()106 private int nonGenericMatcher() { 107 argThat(new NonGenericMatcher()); 108 return 0; 109 } 110 111 @Test coexists_with_mockito_matcher()112 public void coexists_with_mockito_matcher() { 113 when(mock.simpleMethod( 114 Mockito.argThat( 115 new ArgumentMatcher<String>() { 116 public boolean matches(String argument) { 117 return true; 118 } 119 }))) 120 .thenReturn("x"); 121 122 assertEquals("x", mock.simpleMethod("x")); 123 } 124 } 125