Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Hamcrest : Collection contains item of type

Tags:

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?

like image 406
Marty Pitt Avatar asked Feb 13 '11 00:02

Marty Pitt


People also ask

Which of the following is used to check whether all the elements are present in a given list collection in strict order?

To check tha collection contains items in expected (given) order you can use Hamcrest's containsInRelativeOrder method.

Why are there Hamcrest matchers?

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.

Is Hamcrest a matcher?

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.

Is assertThat deprecated?

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.


2 Answers

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))); 
like image 141
Marty Pitt Avatar answered Sep 20 '22 14:09

Marty Pitt


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); 
like image 45
Mike Rylander Avatar answered Sep 21 '22 14:09

Mike Rylander