forked from shazam/shazamcrest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable custom matching on inherited fields
Fixes shazam#15.
- Loading branch information
Showing
2 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/java/com/shazam/shazamcrest/CustomMatcherOnInheritedFieldTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.shazam.shazamcrest; | ||
|
||
import org.junit.Test; | ||
|
||
import static com.shazam.shazamcrest.MatcherAssert.assertThat; | ||
import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs; | ||
import static org.hamcrest.Matchers.startsWith; | ||
|
||
public class CustomMatcherOnInheritedFieldTest { | ||
|
||
@Test | ||
public void inheritedFieldsAreMatchedByCustomMatchers() { | ||
assertThat(new ChildClass(), sameBeanAs(new ChildClass()) | ||
.with("packageProtectedField", startsWith("inherit")) | ||
.with("protectedField", startsWith("inherit")) | ||
.with("publicField", startsWith("inherit")) | ||
); | ||
} | ||
|
||
@Test | ||
public void notInheritedFieldsAreMatchedByCustomMatchers() { | ||
assertThat(new ChildClass(), sameBeanAs(new ChildClass()) | ||
.with("childField", startsWith("child")) | ||
); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void inheritedPrivateFieldsCanNotBeMatched() { | ||
assertThat(new ChildClass(), sameBeanAs(new ChildClass()) | ||
.with("privateField", startsWith("foo")) | ||
); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
private class GrandParentClass { | ||
private String privateField = "not inherited"; | ||
String packageProtectedField = "inherited"; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
private class ParentClass extends GrandParentClass { | ||
protected String protectedField = "inherited"; | ||
public String publicField = "inherited"; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
private class ChildClass extends ParentClass { | ||
private String childField = "child field"; | ||
} | ||
} |