java - Custom hamcrest matcher that works with "not"? -
i have class (say, entity
).
i want able
- test instance of "valid", using custom code decide that
- also test instance not valid, ideally using same code.
using maven, surefire, junit 4.11 (and hamcrest stuff shipped it).
so write class this
class isvalidentity extends typesafematcher<entity>{ @override public boolean matchessafely(entity e){ // , here bunch of asserts... assertnotnull(e.id); // etc. } @override public void describeto(description description) { description.appendtext("is valid entity"); } @factory public static <t> matcher<entity> validentity() { return new isvalidentity(); } }
ok, fine, can do
assertthat(entity, is(validentity());
in junit test, peachy.
but can't do
assertthat(entity, not(validentity());
because validentity
fails broken asserts, while not guess should return false
.
clearly i'm doing backwards here i'm not sure what's clever way of doing these custom matchers. or maybe shouldn't using typesafematcher
@ doing different?
your matchessafely
method should rewritten avoid throwing assertion failures. instead, perform checks manually , return false
if necessary.
then, can negate in manner desire without consequence.
Comments
Post a Comment