Details
-
Type:
New Feature
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: FEST-Assert 2.0
-
Component/s: Assert
-
Labels:None
-
Number of attachments :
Description
Few years ago, I used Unitils and I liked leniant assertions feature (= equals field by field using reflection).
http://unitils.org/tutorial-reflectionassert.html
Now, when at work, we are using a similar idea but implemented with a condition.
I think it could be a method on ObjectAssert.
For me, I propose 3 methods.
Imagining a Jedi object with name/lightsaber color
- Ignoring null
Jedi yoda = new Jedi("yoda", "green"); Jedi yodaTemplate = new Jedi("yoda", null); assertThat(yoda).isLenientEqualsToByIgnoringNull(yodaTemplate);
In yodaTemplate , the lightsaber color is null, so it field is ignore. The equals is just base on name.
Unitils considere default value are ignored. But I don't think it's a good idea. That's why, I think ignoring fields is better.
- Ignoring fields
The idea, is ignoring some fields.
Jedi yoda = new Jedi("yoda", "green"); Jedi yodaTemplate = new Jedi("yoda", "green"); yoda.changeLightsaberColor("blue"); assertThat(yoda).isLenientEqualsToByIgnoringFields(yodaTemplate, "lightsaberColor");
isLenientEqualsToByIgnoringFields without fields params means completely equal.
- Accepting fields
The idea, is accepting some fields.
Jedi yoda = new Jedi("yoda", "green"); Jedi yodaWithAnotherLightsaber = new Jedi("yoda", "blue"); yoda.changeLightsaberColor("blue"); assertThat(yoda).isLenientEqualsToByAcceptingFields(yodaWithAnotherLightsaber , "name");
What do you think on this idea ?
I'am sure the methods name are the best and understandable for users.
I think it's not complicate to implements using PropertySupport.propertyValue (should be public in that way) and provide this method in AbstractAssert.
The 3 methods can delegate to only method on Objects with 3 params : info; actual, and kind of "LenientMatcher".
public interface LenientMatcher{ boolean matches(T expected); }
3 methods just use their own LenientMatcher
objects.assertIsLenientEquals(info, actual, new LenientMatcherIgnoringNull().matches(expected));
I can implements it. Not sure for M1, but soon.
Oups, not a major issue but a minor (I cannot find how edit this issue).