Skip to content
Advertisement

Tag: hamcrest

Assert that actual value is in a collection of possible expected values

Is there a way to assert that an actual value equals any of a number of values? Example: I can see that I can just swap the arguments around: but that reverses the semantics. I’m not checking if permittedTransformations is correct; I’m checking actual. Answer You can map the set elements to individual Matcher<String> objects, and combine them with either:

Hamcrest When to use Is or equalTo

I’m new using hamcrest. While I’m discovering how to use it I have been a doubt about when to use is or equalTo. Is there any difference between is and equalTo, although it is conceptually or ocasionally? It seems to behave the same. Why do you would use one instead of the other? Answer The Javadoc for Matchers is pretty

Is org.junit.Assert.assertThat better than org.hamcrest.MatcherAssert.assertThat?

I’m new to JUnit and Hamcrest and would like best-practice advice so I can decided which documentation to study first. For starters, which of these assertThat methods is better? org.junit.Assert.assertThat (from junit-4.11.jar) org.hamcrest.MatcherAssert.assertThat (from hamcrest-core-1.3.jar) According to one person last year, “JUnit has the assertThat method, but hamcrest has its own assertThat method that does the same thing.”. According to

How to use (primitive) autoboxing/widening with Hamcrest?

I came across https://code.google.com/p/hamcrest/issues/detail?id=130 to add some sugar syntax for Hamcrest matchers. But the idea was rejected by the Hamcrest developers. Any other smart ideas to make tests better readable by avoiding having to type L behind longs? UPDATE See also below the differences when comparing e.g. int and long using default Java laguage (==), standard junit assert (assertTrue) and

Map equality using Hamcrest

I’d like to use hamcrest to assert that two maps are equal, i.e. they have the same set of keys pointing to the same values. My current best guess is: which gives: The method assertThat(T, Matcher<T>) in the type Assert is not applicable for the arguments (Set<Map.Entry<Householdtypes,Double>>, Matcher<Iterable<Set<Map.Entry<Householdtypes,Double>>>>) I’ve also looked into variations of containsAll, and some others provided by

Advertisement