I'd like to assert that List<Achievement>
contains a member of type TestAchievement
.
Here's my assertion:
List<Achievement> achievements; // Populated elsewhere assertThat(achievements,hasItem(isA(TestAchievement.class)));
This doesn't compile, reporting the error:
The method assertThat(T, Matcher) in the type Assert is not applicable for the arguments (List, Matcher<Iterable<TestAchievement>>)
What's the correct syntax for this type of assertion using Hamcrest?
To check tha collection contains items in expected (given) order you can use Hamcrest's containsInRelativeOrder method.
Purpose of the Hamcrest matcher framework. Hamcrest is a widely used framework for unit testing in the Java world. Hamcrest target is to make your tests easier to write and read. For this, it provides additional matcher classes which can be used in test for example written with JUnit.
Introduction. Hamcrest is a framework for writing matcher objects allowing 'match' rules to be defined declaratively. There are a number of situations where matchers are invaluable, such as UI validation or data filtering, but it is in the area of writing flexible tests that matchers are most commonly used.
assertThat method is deprecated. Its sole purpose is to forward the call to the MatcherAssert. assertThat defined in Hamcrest 1.3. Therefore, it is recommended to directly use the equivalent assertion defined in the third party Hamcrest library.
Thanks for all the help.
The posts here suggested it was a defect with Hamcrest, so I headed over to the hacmrest site to register a bug, whien I discovered that the mvn / ivy dependency declaration I was using was out-of-date, giving me an old version of Hamcrest.
This bug exists with 1.1, which is the latest if declared using
<dependency org="org.hamcrest" name="hamcrest-all" rev="1.1">
However, the correct depedency declaration is:
<dependency org="org.hamcrest" name="hamcrest-library" rev="1.3.RC2"/>
Updating to this solved the issue. The syntax used in my test is:
assertThat(achievements, hasItem(isA(TestAchievement.class)));
There is a bug in Java 6 that relates to this.
This code will throw various errors such as "cannot find symbol..."
assertThat(achievements, hasItem(isA(TestAchievement.class)));
The work around for this is to declare the matcher as a variable and then reference that variable. It is important to note that the type of the variable, specifically the generics section, is very important for this to work.
Matcher<Iterable<? super TestAchievement>> matcher = hasItem(isA(TestAchievement.class)); assertThat(achievements, matcher);
Interestingly if you use instanceOf()
instead of isA()
you once again run into issue. (although if you ignore the warnings this might just work for you anyway.) Expanding on the previous fix you can use:
Matcher<TestAchievement> itemMatcher = Matchers.instanceOf(TestAchievement.class); Matcher<Iterable<? super TestAchievement>> matcher = hasItem(itemMatcher); assertThat(achievements, matcher);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With