java - Is there a simple way to match a field using Hamcrest? -


i want test whether specific field of object matches value specify. in case, it's bucket name inside s3bucket object. far can tell, need write custom matcher this:

mockery.checking(new expectations() {{   one(query.s3).getobject(with(       new basematcher<s3bucket>() {         @override         public boolean matches(object item) {           if (item instanceof s3bucket) {             return ((s3bucket)item).getname().equals("bucket");           } else {             return false;           }         }         @override         public void describeto(description description) {           description.appendtext("bucket name isn't \"bucket\"");         }       }), with(equal("key")));     ...     }}); 

it nice if there simpler way this, like:

mockery.checking(new expectations() {{   one(query.s3).getobject(     with(equal(methodof(s3bucket.class).getname(), "bucket")),     with(equal("key")));     ... }}); 

can point me that? guess i've solved problem in case, isn't first time i've wished simpler way.

alternatively, more typesafe version, there's featurematcher. in case, like:

private matcher<s3bucket> bucketname(final string expected) {   return new featurematcher<s3bucket, string>(equalto(expected),                                                "bucket called", "name") {      string featurevalueof(s3bucket actual) {        return actual.getname();      }   }; } 

giving:

mockery.checking(new expectations() {{   one(query.s3).getobject(with(bucketname("bucket")), with(equalto("key")));     ... }}); 

the purpose of 2 string arguments make mismatch report read well.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -