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.junit.Before; 9 import org.junit.Test; 10 import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent; 11 import org.mockitoutil.TestBase; 12 13 import static org.mockito.Matchers.refEq; 14 import static org.mockito.Mockito.mock; 15 import static org.mockito.Mockito.verify; 16 17 @SuppressWarnings("all") 18 public class ReflectionMatchersTest extends TestBase { 19 20 class Parent { 21 private int parentField; 22 protected String protectedParentField; Parent(int parentField, String protectedParentField)23 public Parent(int parentField, String protectedParentField) { 24 this.parentField = parentField; 25 this.protectedParentField = protectedParentField; 26 } 27 } 28 29 class Child extends Parent { 30 private int childFieldOne; 31 private Object childFieldTwo; Child(int parentField, String protectedParentField, int childFieldOne, Object childFieldTwo)32 public Child(int parentField, String protectedParentField, int childFieldOne, Object childFieldTwo) { 33 super(parentField, protectedParentField); 34 this.childFieldOne = childFieldOne; 35 this.childFieldTwo = childFieldTwo; 36 } 37 } 38 39 interface MockMe { run(Child child)40 void run(Child child); 41 } 42 43 MockMe mock; 44 45 @Before setup()46 public void setup() { 47 mock = mock(MockMe.class); 48 49 Child actual = new Child(1, "foo", 2, "bar"); 50 mock.run(actual); 51 } 52 53 @Test shouldMatchWhenFieldValuesEqual()54 public void shouldMatchWhenFieldValuesEqual() throws Exception { 55 Child wanted = new Child(1, "foo", 2, "bar"); 56 verify(mock).run(refEq(wanted)); 57 } 58 59 @Test(expected=ArgumentsAreDifferent.class) shouldNotMatchWhenFieldValuesDiffer()60 public void shouldNotMatchWhenFieldValuesDiffer() throws Exception { 61 Child wanted = new Child(1, "foo", 2, "bar XXX"); 62 verify(mock).run(refEq(wanted)); 63 } 64 65 @Test(expected=ArgumentsAreDifferent.class) shouldNotMatchAgain()66 public void shouldNotMatchAgain() throws Exception { 67 Child wanted = new Child(1, "foo", 999, "bar"); 68 verify(mock).run(refEq(wanted)); 69 } 70 71 @Test(expected=ArgumentsAreDifferent.class) shouldNotMatchYetAgain()72 public void shouldNotMatchYetAgain() throws Exception { 73 Child wanted = new Child(1, "XXXXX", 2, "bar"); 74 verify(mock).run(refEq(wanted)); 75 } 76 77 @Test(expected=ArgumentsAreDifferent.class) shouldNotMatch()78 public void shouldNotMatch() throws Exception { 79 Child wanted = new Child(234234, "foo", 2, "bar"); 80 verify(mock).run(refEq(wanted)); 81 } 82 83 @Test shouldMatchWhenFieldValuesEqualWithOneFieldExcluded()84 public void shouldMatchWhenFieldValuesEqualWithOneFieldExcluded() throws Exception { 85 Child wanted = new Child(1, "foo", 2, "excluded"); 86 verify(mock).run(refEq(wanted, "childFieldTwo")); 87 } 88 89 @Test shouldMatchWhenFieldValuesEqualWithTwoFieldsExcluded()90 public void shouldMatchWhenFieldValuesEqualWithTwoFieldsExcluded() throws Exception { 91 Child wanted = new Child(234234, "foo", 2, "excluded"); 92 verify(mock).run(refEq(wanted, "childFieldTwo", "parentField")); 93 verify(mock).run(refEq(wanted, "parentField", "childFieldTwo")); 94 } 95 96 @Test(expected=ArgumentsAreDifferent.class) shouldNotMatchWithFieldsExclusion()97 public void shouldNotMatchWithFieldsExclusion() throws Exception { 98 Child wanted = new Child(234234, "foo", 2, "excluded"); 99 verify(mock).run(refEq(wanted, "childFieldTwo")); 100 } 101 } 102