【问题标题】:Using hamcrest to assert that I get a collection of Objects back and that one of them has a property set to a particular value? [closed]使用 hamcrest 断言我得到了一组对象,并且其中一个对象的属性设置为特定值? [关闭]
【发布时间】:2014-10-24 10:59:55
【问题描述】:

我刚开始使用 Hamcrest,所以我可能做错了。

我有一个List<Foo> foosFoo 界面看起来有点像这样:

public abstract interface Foo {

  public String getBar();

}

impl.FooImpl实现:

public class FooImpl implements Foo {

  protected String _bar = "some value";

  public String getBar() {
    return _bar;
  }

}

我的断言如下所示:

assertThat(
  foos, 
  Matchers.hasItem(
    Matchers.<Foo> hasProperty(
      "bar", 
      equalTo("some value")
    )
  )
);

不幸的是,JUnit/Hamcrest 并不高兴:

java.lang.AssertionError: 
Expected: a collection containing hasProperty("bar", "someValue")
     got: <[com.example.impl.FooImpl@2c78bc3b]>

知道我需要做什么来解决这个问题吗?

更新:我的“测试”课程在这里:

public class FooTest {
  public static void main(String... args) throws Exception {
    List<Foo> foos = Arrays.<Foo> asList(new FooImpl());
      assertThat(
        foos, 
        Matchers.<FooImpl> hasItem(Matchers.hasProperty("bar", equalTo("some value")))
      );
  }
}

显然,我希望看到断言通过而没有异常... :)

更新:现已修复;我刚刚在 IntelliJ 中设置了一个空白的 maven 项目,它运行良好。可能是因为拼写错误,或者我在 Eclipse 中导入了错误的方法,但这肯定是 OSI 第 8 层的问题。我要求结束谈话。对不起大家,感谢您的帮助。

【问题讨论】:

  • 您的住宿名称是“bar”,而不是“_bar”。 “_bar”是您的私有字段的名称。属性的名称是 getter 中 get 之后的名称。
  • 为什么要关闭投票?如果这不是 SO 的用途,那么我不知道是什么
  • 我现在将它交叉发布到 groups.google.com/d/msg/hamcrest-java/AmZGLq0Kby0/ojd9XbbxjGUJ 的 hamcrest 用户组,因为显然有人认为它不属于这里......
  • @Christian 目前你有一个接近的投票,它表示问题“必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码我>”。如果您不提供问题的可编译示例,您可能会继续获得如此接近的投票。没有什么比可重现的问题更能加速答案的出现了。
  • 能否请您使用 Hamcrest 1.3 的 assertThat。它会打印有关失败原因的更多详细信息。

标签: java junit assert hamcrest


【解决方案1】:

您发布的代码运行良好。您遇到的错误是由于您在本地运行的代码中的"someValue""some value" 不匹配。升级到 Hamcrest 1.3,您会收到更清晰的错误消息:

Expected: a collection containing hasProperty("bar", "someValue")
    but: property 'bar' was 'some value'

由于您可能会得到不同的结果,这里有一个完整的示例,使用 JUnit 4.11 和 Hamcrest 1.3。在Foo.java:

public class Foo {
    public String getBar() {
        return "some value";
    }
}

FooTest.java:

import static org.junit.Assert.assertThat;

import java.util.Arrays;
import java.util.List;

import org.hamcrest.Matchers;
import org.junit.Test;

public class FooTest {
    @Test
    public void test() {
        List<Foo> foos = Arrays.asList(new Foo());

        assertThat(foos,
                Matchers.<Foo>hasItem(Matchers.hasProperty("bar", Matchers.equalTo("some value"))));
    }
}

如果我故意通过将测试中的属性名称更改为 "notbar" 来破坏此测试,那么我会得到:

java.lang.AssertionError: 
Expected: a collection containing hasProperty("notbar", "some value")
     but: No property "notbar"

如果相反,我通过期待 "some other value" 来打破它,那么我得到:

java.lang.AssertionError: 
Expected: a collection containing hasProperty("bar", "some other value")
     but: property 'bar' was "some value"

如果您遇到不同或不太清楚的错误,则可能是另一个问题。

【讨论】:

  • 看来你是对的。我刚刚在 IntelliJ 中创建了一个空白的 maven 项目,并使用了我上面的代码形式,它工作正常。我将不得不检查 Eclipse 中的真正问题是使用了错误的导入还是实际的拼写错误,但这可能是次要的。感谢您发现这一点。 :)
  • 所以,我的属性称为_FooBar,getter 称为getFooBar(),正确的匹配器是Matchers.equalTo( "fooBar", "some value")... (注意小写字母“f” .) 此外,hamcrest 1.3 的错误消息毕竟并没有更好...... :)
  • 我的 getter 和 setter 不公开,mockito 让我头疼了 2/3 小时……让测试用例中使用的属性公开 getter 解决了这个问题
猜你喜欢
  • 1970-01-01
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
  • 2016-08-05
  • 1970-01-01
  • 2015-11-12
  • 2022-01-06
相关资源
最近更新 更多