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