hamcrest

Checking that a List is not empty in Hamcrest

青春壹個敷衍的年華 提交于 2019-11-27 09:42:32
问题 I was wondering if anyone knew of a way to check if a List is empty using assertThat() and Matchers ? Best way I could see just use JUnit: assertFalse(list.isEmpty()); But I was hoping that there was some way to do this in Hamcrest. 回答1: Well there's always assertThat(list.isEmpty(), is(false)); ... but I'm guessing that's not quite what you meant :) Alternatively: assertThat((Collection)list, is(not(empty()))); empty() is a static in the Matchers class. Note the need to cast the list to

How can I use Hamcrest to check if each element in an array of doubles is “close” to each element in another array?

人盡茶涼 提交于 2019-11-27 06:47:30
问题 I would like to compare two arrays of doubles. Using vanilla JUnit, I can do: double[] a = new double[]{1.0, 2.0, 3.0}; double[] b = new double[]{1.0, 2.0, 3.0}; assertEquals(a, b, 1e-10); I would like to know how to do this using Hamcrest, preferably without creating custom Matchers (if possible). Something akin to using the "close" matcher for each element in an array. 回答1: If you change a to a Double[] then you can do assertThat(a, arrayCloseTo(b, .2)); with this helper method: public

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

孤街浪徒 提交于 2019-11-27 03:40:47
问题 I want to test whether a specific field of an object matches a value I specify. In this case, it's the bucket name inside an S3Bucket object. As far as I can tell, I need to write a custom matcher for 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

How do I assert an Iterable contains elements with a certain property?

橙三吉。 提交于 2019-11-27 00:45:37
Assume I want to unit test a method with this signature: List<MyItem> getMyItems(); Assume MyItem is a Pojo that has many properties, one of which is "name" , accessed via getName() . All I care about verifying is that the List<MyItem> , or any Iterable , contains two MyItem instances, whose "name" properties have the values "foo" and "bar" . If any other properties don't match, I don't really care for the purposes of this test. If the names match, it's a successful test. I would like it to be one-liner if possible. Here is some "pseudo-syntax" of the kind of thing I would like to do. assert

How to assert that a list has at least n items which are greater than x (with hamcrest in junit)

♀尐吖头ヾ 提交于 2019-11-26 23:33:07
问题 I could with following code check if a list has a item, which greater than 30. //Using Hamcrest List<Integer> ints= Arrays.asList(22,33,44,55); assertThat(ints,hasItem(greaterThan(30))); But how could I assert if a list has at least 2 items, which are greater than 30? With AssertJ , there is a solution I know. But I have no idea how to realize that with Hamcrest . //Using AssertJ List<Integer> ints= Arrays.asList(22,33,44,55); Condition<Integer> greaterThanCondition = new Condition<Integer>(

Mockito's Matcher vs Hamcrest Matcher?

自闭症网瘾萝莉.ら 提交于 2019-11-26 19:42:49
问题 That's going to be an easy one, but I cannot find the difference between them and which one to use, if I have both the lib's included in my classpath? 回答1: Hamcrest matcher methods return Matcher<T> and Mockito matchers return T. So, for example: org.hamcrest.Matchers.any(Integer.class) returns an instance of org.hamcrest.Matcher<Integer> , and org.mockito.Matchers.any(Integer.class) returns an instance of Integer . That means that you can only use Hamcrest matchers when a Matcher<?> object

Hamcrest compare collections

别等时光非礼了梦想. 提交于 2019-11-26 18:39:59
I'm trying to compare 2 lists: assertThat(actual.getList(), is(Matchers.containsInAnyOrder(expectedList))); But idea java: no suitable method found for assertThat(java.util.List<Agent>,org.hamcrest.Matcher<java.lang.Iterable<? extends model.Agents>>) method org.junit.Assert.<T>assertThat(T,org.hamcrest.Matcher<T>) is not applicable (no instance(s) of type variable(s) T exist so that argument type org.hamcrest.Matcher<java.lang.Iterable<? extends model.Agents>> conforms to formal parameter type org.hamcrest.Matcher<T>) method org.junit.Assert.<T>assertThat(java.lang.String,T,org.hamcrest

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

泪湿孤枕 提交于 2019-11-26 17:34:26
While running junit test in eclipse I am getting this Exception : java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing I've added junit.jar library file. I've tried different versions of junit.jar: 4.4 , 4.8 , etc. How do I fix this Exception? Jiacai Liu Add hamcrest-all-X.X.jar to your classpath . Latest version as of Feb 2015 is 1.3: http://code.google.com/p/hamcrest/downloads/detail?name=hamcrest-all-1.3.jar&can=2&q= According to the JUnit GitHub team website ( https://github.com/junit-team/junit/wiki/Download-and-Install ), junit.jar and hamcrest-core.jar are both needed in the

hamcrest tests always fail

时光毁灭记忆、已成空白 提交于 2019-11-26 16:00:05
问题 I am using hamcrest 1.3 to test my code. It is simply a die. I am trying to test it to make sure the number generated is less than 13. I had a print statement that printed what the number generated was. The number generated was always less than 13 but the test always failed. Is there something I am doing wrong? This is the code I am testing. import java.util.Random; public class Die { private int numSides; Random rand; public Die(int numSides){ this.numSides = numSides; rand = new Random

Getting “NoSuchMethodError: org.hamcrest.Matcher.describeMismatch” when running test in IntelliJ 10.5

我怕爱的太早我们不能终老 提交于 2019-11-26 15:06:59
I'm using JUnit-dep 4.10 and Hamcrest 1.3.RC2. I've created a custom matcher that looks like the following: 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 */ } } It works perfectly fine when run from the command line using Ant. But when run from IntelliJ, it fails with: java.lang.NoSuchMethodError: org