hamcrest

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-28 12:02:58
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. If you change a to a Double[] then you can do assertThat(a, arrayCloseTo(b, .2)); with this helper method: public static Matcher<Double[]> arrayCloseTo(double[] array, double error) { List<Matcher<? super Double>> matchers =

How to test enum types?

我只是一个虾纸丫 提交于 2019-11-28 09:47:50
I'm currently trying to build a more or less complete set of unit tests for a small library. Since we want to allow different implementations to exist we want this set of tests to be (a) generic, so that we can re-use it to test the different implementations and (b) as complete as possible. For the (b) part I'd like to know if there is any best-practice out there for testing enum types. So for example I have an enum as follows: public enum Month { January, February, ... December; } Here I want to ensure that all enum types really exist. Is that even necessary? Currently I'm using Hamcrests

How to use JUnit and Hamcrest together?

蹲街弑〆低调 提交于 2019-11-28 03:17:48
I can't understand how JUnit 4.8 should work with Hamcrest matchers. There are some matchers defined inside junit-4.8.jar in org.hamcrest.CoreMatchers . At the same time there are some other matchers in hamcrest-all-1.1.jar in org.hamcrest.Matchers . So, where to go? Shall I explicitly include hamcrest JAR into the project and ignore matchers provided by JUnit? In particular, I'm interested in empty() matcher and can't find it in any of these jars. I need something else? :) And a philosophical question: why JUnit included org.hamcrest package into its own distribution instead of encouraging us

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

三世轮回 提交于 2019-11-28 01:33:27
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>("greater") { @Override public boolean matches (Integer i){ return i>30; } } ; assertThat(ints)

Mockito's Matcher vs Hamcrest Matcher?

£可爱£侵袭症+ 提交于 2019-11-27 19:00:15
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? 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 is expected in the signature - typically, in assertThat calls. When setting up expectations or verifications

How to check if collection contains items in given order using Hamcrest

淺唱寂寞╮ 提交于 2019-11-27 14:20:01
问题 How to check using Hamcrest if given collection is containing given items in given order? I tried hasItems but it simply ignores the order. List<String> list = Arrays.asList("foo", "bar", "boo"); assertThat(list, hasItems("foo", "boo")); //I want this to fail, because the order is different than in "list" assertThat(list, hasItems("boo", "foo")); 回答1: You can use contains matcher instead, but you probably need to use latest version of Hamcrest. That method checks the order. assertThat(list,

What is the idiomatic Hamcrest pattern to assert that each element of an iterable matches a given matcher?

戏子无情 提交于 2019-11-27 12:53:53
问题 Examine the following snippet: assertThat( Arrays.asList("1x", "2x", "3x", "4z"), not(hasItem(not(endsWith("x")))) ); This asserts that the list doesn't have an element that doesn't end with "x". This, of course, is the double negatives way of saying that all elements of the list ends with "x". Also note that the snippet throws: java.lang.AssertionError: Expected: not a collection containing not a string ending with "x" got: <[1x, 2x, 3x, 4z]> This lists the entire list, instead of just the

Why doesn't this code attempting to use Hamcrest's hasItems compile?

浪尽此生 提交于 2019-11-27 12:33:08
Why does this not compile, oh, what to do? import static org.junit.Assert.assertThat; import static org.junit.matchers.JUnitMatchers.hasItems; ArrayList<Integer> actual = new ArrayList<Integer>(); ArrayList<Integer> expected = new ArrayList<Integer>(); actual.add(1); expected.add(2); assertThat(actual, hasItems(expected)); error copied from comment: cannot find symbol method assertThat(java.util.ArrayList<java.lang.Integer>, org.hamcreset.Matcher<java.lang.Iterable<java.util.ArrayList<java.lang.Integer>>>) Clive Evans Just ran into this post trying to fix it for myself. Gave me just enough

hamcrest tests always fail

穿精又带淫゛_ 提交于 2019-11-27 12:18:06
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(System.currentTimeMillis()); } public int roll(){ return rand.nextInt(numSides) + 1; } } And this is my

How do Hamcrest's hasItems, contains and containsInAnyOrder differ?

夙愿已清 提交于 2019-11-27 09:57:27
问题 Hamcrest provides a number of matchers for asserting the contents of a collection. All of these cases pass: Collection<String> c = ImmutableList.of("one", "two", "three"); assertThat(c, hasItems("one", "two", "three"); assertThat(c, contains("one", "two", "three"); assertThat(c, containsInAnyOrder("one", "two", "three"); How do hasItems , contains and containsInAnyOrder differ? 回答1: hasItems checks: consecutive passes over the examined Iterable yield at least one item that is equal to the