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.assertThatThrownBy; 8 import static org.mockito.ArgumentMatchers.refEq; 9 import static org.mockito.Mockito.mock; 10 import static org.mockito.Mockito.verify; 11 12 import org.junit.Before; 13 import org.junit.Test; 14 import org.mockito.exceptions.verification.opentest4j.ArgumentsAreDifferent; 15 import org.mockitoutil.TestBase; 16 17 @SuppressWarnings("all") 18 public class ReflectionMatchersTest extends TestBase { 19 20 class Parent { 21 private int parentField; 22 protected String protectedParentField; 23 Parent(int parentField, String protectedParentField)24 public Parent(int parentField, String protectedParentField) { 25 this.parentField = parentField; 26 this.protectedParentField = protectedParentField; 27 } 28 } 29 30 class Child extends Parent { 31 private int childFieldOne; 32 private Object childFieldTwo; 33 Child( int parentField, String protectedParentField, int childFieldOne, Object childFieldTwo)34 public Child( 35 int parentField, 36 String protectedParentField, 37 int childFieldOne, 38 Object childFieldTwo) { 39 super(parentField, protectedParentField); 40 this.childFieldOne = childFieldOne; 41 this.childFieldTwo = childFieldTwo; 42 } 43 } 44 45 interface MockMe { run(Child child)46 void run(Child child); 47 } 48 49 MockMe mock; 50 51 @Before setup()52 public void setup() { 53 mock = mock(MockMe.class); 54 55 Child actual = new Child(1, "foo", 2, "bar"); 56 mock.run(actual); 57 } 58 59 @Test shouldMatchWhenFieldValuesEqual()60 public void shouldMatchWhenFieldValuesEqual() throws Exception { 61 Child wanted = new Child(1, "foo", 2, "bar"); 62 verify(mock).run(refEq(wanted)); 63 } 64 65 @Test shouldNotMatchWhenFieldValuesDiffer()66 public void shouldNotMatchWhenFieldValuesDiffer() throws Exception { 67 Child wanted = new Child(1, "foo", 2, "bar XXX"); 68 assertThatThrownBy( 69 () -> { 70 verify(mock).run(refEq(wanted)); 71 }) 72 .isInstanceOf(ArgumentsAreDifferent.class) 73 .hasMessageContainingAll( 74 "Argument(s) are different! Wanted:", 75 "mockMe.run(", 76 " refEq(org.mockitousage.matchers.ReflectionMatchersTest", 77 "Actual invocations have different arguments:", 78 "mockMe.run(", 79 " org.mockitousage.matchers.ReflectionMatchersTest"); 80 } 81 82 @Test shouldNotMatchAgain()83 public void shouldNotMatchAgain() throws Exception { 84 Child wanted = new Child(1, "foo", 999, "bar"); 85 assertThatThrownBy( 86 () -> { 87 verify(mock).run(refEq(wanted)); 88 }) 89 .isInstanceOf(ArgumentsAreDifferent.class) 90 .hasMessageContainingAll( 91 "Argument(s) are different! Wanted:", 92 "mockMe.run(", 93 " refEq(org.mockitousage.matchers.ReflectionMatchersTest", 94 "Actual invocations have different arguments:", 95 "mockMe.run(", 96 " org.mockitousage.matchers.ReflectionMatchersTest"); 97 } 98 99 @Test shouldNotMatchYetAgain()100 public void shouldNotMatchYetAgain() throws Exception { 101 Child wanted = new Child(1, "XXXXX", 2, "bar"); 102 assertThatThrownBy( 103 () -> { 104 verify(mock).run(refEq(wanted)); 105 }) 106 .isInstanceOf(ArgumentsAreDifferent.class) 107 .hasMessageContainingAll( 108 "Argument(s) are different! Wanted:", 109 "mockMe.run(", 110 " refEq(org.mockitousage.matchers.ReflectionMatchersTest", 111 "Actual invocations have different arguments:", 112 "mockMe.run(", 113 " org.mockitousage.matchers.ReflectionMatchersTest"); 114 } 115 116 @Test shouldNotMatch()117 public void shouldNotMatch() throws Exception { 118 Child wanted = new Child(234234, "foo", 2, "bar"); 119 assertThatThrownBy( 120 () -> { 121 verify(mock).run(refEq(wanted)); 122 }) 123 .isInstanceOf(ArgumentsAreDifferent.class) 124 .hasMessageContainingAll( 125 "Argument(s) are different! Wanted:", 126 "mockMe.run(", 127 " refEq(org.mockitousage.matchers.ReflectionMatchersTest", 128 "Actual invocations have different arguments:", 129 "mockMe.run(", 130 " org.mockitousage.matchers.ReflectionMatchersTest"); 131 } 132 133 @Test shouldMatchWhenFieldValuesEqualWithOneFieldExcluded()134 public void shouldMatchWhenFieldValuesEqualWithOneFieldExcluded() throws Exception { 135 Child wanted = new Child(1, "foo", 2, "excluded"); 136 verify(mock).run(refEq(wanted, "childFieldTwo")); 137 } 138 139 @Test shouldMatchWhenFieldValuesEqualWithTwoFieldsExcluded()140 public void shouldMatchWhenFieldValuesEqualWithTwoFieldsExcluded() throws Exception { 141 Child wanted = new Child(234234, "foo", 2, "excluded"); 142 verify(mock).run(refEq(wanted, "childFieldTwo", "parentField")); 143 verify(mock).run(refEq(wanted, "parentField", "childFieldTwo")); 144 } 145 146 @Test shouldNotMatchWithFieldsExclusion()147 public void shouldNotMatchWithFieldsExclusion() throws Exception { 148 Child wanted = new Child(234234, "foo", 2, "excluded"); 149 assertThatThrownBy( 150 () -> { 151 verify(mock).run(refEq(wanted, "childFieldTwo")); 152 }) 153 .isInstanceOf(ArgumentsAreDifferent.class) 154 .hasMessageContainingAll( 155 "Argument(s) are different! Wanted:", 156 "mockMe.run(", 157 " refEq(org.mockitousage.matchers.ReflectionMatchersTest", 158 "Actual invocations have different arguments:", 159 "mockMe.run(", 160 " org.mockitousage.matchers.ReflectionMatchersTest"); 161 } 162 } 163