Tag: hamcrest

如何声明一个Iterable包含具有某个属性的元素?

假设我想用这个签名unit testing一个方法: List<MyItem> getMyItems(); 假设MyItem是一个有很多属性的Pojo,其中之一是通过getName()访问的"name" 。 我关心的是, List<MyItem>或任何Iterable包含两个MyItem实例,其"name"属性的值为"foo"和"bar" 。 如果任何其他属性不匹配,我真的不在乎这个testing的目的。 如果名字匹配,这是一个成功的testing。 如果可能的话,我希望它是单行的。 这是我想做的事情的一些“伪语法”。 assert(listEntriesMatchInAnyOrder(myClass.getMyItems(), property("name"), new String[]{"foo", "bar"}); Hamcrest会对这种事情有好处吗? 如果是这样,那么我的伪语法的最后一个版本是什么?

java.lang.NoClassDefFoundError:org / hamcrest / SelfDescribing

在eclipse运行junittesting时,我得到这个Exception : java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing 我已经添加了junit.jar库文件。 我试过不同版本的junit.jar:4.4,4.8等 我如何解决这个例外?

如何一起使用JUnit和Hamcrest?

我无法理解JUnit 4.8应该如何与Hamcrest匹配器一起工作。 org.hamcrest.CoreMatchers junit-4.8.jar中定义了一些匹配器。 同时, org.hamcrest.Matchers 其他一些matcher也在hamcrest-all-1.1.jar中。 那么,去哪里? 我应该明确地将Hamcrest JAR包含到项目中,而忽略JUnit提供的匹配器吗? 特别是,我对empty()匹配器感兴趣,并且无法在这些jar中find它。 我需要别的东西? 🙂 还有一个哲学问题:为什么JUnit将org.hamcrest包纳入自己的发行版,而不是鼓励我们使用原始的hamcrest库?

如何断言与Hamcrest什么是空的?

我将如何assertThat什么是null ? 例如 assertThat(attr.getValue(), is("")); 但是我得到一个错误,说我不能在null is(null)中is(null) 。

在Hamcrest中检查列表是否为空

我想知道是否有人知道使用assertThat()和Matchers来检查List是否为空的方法? 我可以看到使用JUnit的最佳方式: assertFalse(list.isEmpty()); 但我希望在Hamcrest有办法做到这一点。

为什么要使用Hamcrest-Matcher和assertThat()而不是传统的assertXXX() – 方法

当我看着Assert类的JavaDoc中的例子 assertThat("Help! Integers don't work", 0, is(1)); // fails: // failure message: // Help! Integers don't work // expected: is <1> // got value: <0> assertThat("Zero is one", 0, is(not(1))) // passes 我没有看到一个很大的优势,比方说assertEquals( 0, 1 ) 。 如果结构变得更复杂,但是你看到更多的优势,那么也许是好消息? 可读性?

在IntelliJ 10.5中运行testing时获取“NoSuchMethodError:org.hamcrest.Matcher.describeMismatch”

我正在使用JUnit-dep 4.10和Hamcrest 1.3.RC2。 我创build了一个如下所示的自定义匹配器: public static class MyMatcher extends TypeSafeMatcher<String> { @Override protected boolean matchesSafely(String s) { /* implementation */ } @Override public void describeTo(Description description) { /* implementation */ } @Override protected void describeMismatchSafely(String item, Description mismatchDescription) { /* implementation */ } } 从命令行使用Ant运行时,它工作得很好。 但是从IntelliJ运行时,它会失败: java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18) at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8) at com.netflix.build.MyTest.testmyStuff(MyTest.java:40) 我的猜测是它使用了错误的hamcrest.MatcherAssert。 […]