我想测试对象的特定字段是否与我指定的值匹配。在这种情况下,它是S3bucket对象中的桶名称。据我所知,我需要为此编写自定义匹配器:

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")));
    ...
    }});
.

如果有更简单的方法来做这件事,那就很好了,如:

mockery.checking(new Expectations() {{
  one(query.s3).getObject(
    with(equal(methodOf(S3Bucket.class).getName(), "bucket")),
    with(equal("key")));
    ...
}});
.

谁能指出我这样的东西?我想我已经解决了我的问题已经在这种情况下,但这不是我第一次希望出于更简单的方式。

有帮助吗?

解决方案

Alternatively, for a more typesafe version, there's the FeatureMatcher. In this case, something 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 the two string arguments is to make the mismatch report read well.

其他提示

Sounds like you need to use Matchers.hasProperty, e.g.

mockery.checking(new Expectations() {{
  one(query.s3).getObject(
    with(hasProperty("name", "bucket")),
    with(equal("key")));
    ...
}});

Or something similar.

There is a neat way of doing this with LambdaJ:

mockery.checking(new Expectations() {{
  one(query.s3).getObject(
    with(having(on(S3Bucket.class).getName(), is("bucket")))
  )
}});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top