Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用hamcrest断言我得到了一个对象集合,并且其中一个对象的属性设置为特定值?_Java_Junit_Assert_Hamcrest - Fatal编程技术网

Java 使用hamcrest断言我得到了一个对象集合,并且其中一个对象的属性设置为特定值?

Java 使用hamcrest断言我得到了一个对象集合,并且其中一个对象的属性设置为特定值?,java,junit,assert,hamcrest,Java,Junit,Assert,Hamcrest,我刚开始使用Hamcrest,所以我可能完全做错了 我有一个列表foos,而Foo界面看起来有点像这样: public abstract interface Foo { public String getBar(); } assertThat( foos, Matchers.hasItem( Matchers.<Foo> hasProperty( "bar", equalTo("some value") ) ) );

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

我有一个
列表foos
,而
Foo
界面看起来有点像这样:

public abstract interface Foo {

  public String getBar();

}
assertThat(
  foos, 
  Matchers.hasItem(
    Matchers.<Foo> hasProperty(
      "bar", 
      equalTo("some value")
    )
  )
);
它由
impl.FooImpl
实现:

public class FooImpl implements Foo {

  protected String _bar = "some value";

  public String getBar() {
    return _bar;
  }

}
我的断言如下所示:

public abstract interface Foo {

  public String getBar();

}
assertThat(
  foos, 
  Matchers.hasItem(
    Matchers.<Foo> hasProperty(
      "bar", 
      equalTo("some value")
    )
  )
);
断言(
福斯,
Matchers.hasItem(
Matchers.hasProperty(
“酒吧”,
equalTo(“某种价值”)
)
)
);
不幸的是,JUnit/Hamcrest并不高兴:

java.lang.AssertionError: 
Expected: a collection containing hasProperty("bar", "someValue")
     got: <[com.example.impl.FooImpl@2c78bc3b]>
java.lang.AssertionError:
应为:包含hasProperty(“bar”、“someValue”)的集合
得到了:
你知道我需要做什么来解决这个问题吗

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

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")))
      );
  }
}
public-class-FooTest{
公共静态void main(字符串…参数)引发异常{
listfoos=Arrays.asList(newfooimpl());
断言(
福斯,
Matchers.hasItem(Matchers.hasProperty(“bar”),equalTo(“some value”))
);
}
}
显然,我希望看到的是,assert可以在不获得异常的情况下通过…:)


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

您发布的代码工作正常。您的错误是由于本地运行的代码中的
“someValue”
“someValue”
不匹配造成的。升级到Hamcrest 1.3,您将得到更清晰的错误消息:

Expected: a collection containing hasProperty("bar", "someValue")
    but: property 'bar' was 'some value'
由于您可能会得到不同的结果,下面是一个通过JUnit4.11和Hamcrest 1.3的完整示例。在
Foo.java
中:

public class Foo {
    public String getBar() {
        return "some value";
    }
}
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"))));
    }
}
FooTest.java
中:

public class Foo {
    public String getBar() {
        return "some value";
    }
}
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"))));
    }
}
相反,如果我通过期望
“其他值”
来打破它,那么我得到:

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

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

您的属性名称是“bar”,而不是“u bar”。“\u bar”是您的专用字段的名称。属性的名称是getter中的
get
之后的名称。为什么要投票关闭?如果这不是这么回事,那么我不知道isI现在把它交叉发布到了hamcrest的用户群中,因为有人显然认为它不属于这里…@Christian目前你有一票接近,这表明问题“必须包括期望的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码“。如果您没有提供问题的可编译示例,您可能会继续获得如此接近的票数。没有什么比重现性问题更能加速答案的得出。请使用Hamcrest 1.3的资产。它会打印出更多关于失败原因的详细信息。看起来你是对的。我刚刚在IntelliJ中创建了一个空白的maven项目,并使用了上面的代码表单,效果很好。我必须检查Eclipse中真正的问题是什么,是使用了错误的导入还是实际的输入错误,但这可能是次要的。谢谢你发现这一点。:)因此,我的属性被称为
\u FooBar
,getter被称为
getFooBar()
,正确的匹配器是
Matchers.equalTo(“FooBar”,“some value”)
。。。(注意小写的“f”。)同样,hamcrest 1.3的错误消息也没有任何改善,毕竟…:)我的能手和二传手并没有公开,莫基托让我在头上撞了2/3个小时。。。将getter公开给testcase中使用的属性解决了这个问题