Question

Running

assertThat(collection, allOf(hasItems(i1, i2, i3), hasSize(3)));

from Eclipse (Run as -> Junit) everything works fine, but when I execute Maven test (mvn clean test) it fails during test-compile phase with the following explanation

[ERROR] The method allOf(Matcher<? super T>, Matcher<? super T>) in the type AllOf<T> is not applicable for the arguments (Matcher<Iterable<Song>>, Matcher<Collection<? extends Object>>)

The dependencies are

<dependency>
  <groupId>org.hamcrest</groupId>
  <artifactId>hamcrest-all</artifactId>
  <version>1.3</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit-dep</artifactId>
  <version>4.10</version>
  <scope>test</scope>
  <exclusions>
    <exclusion>
      <artifactId>hamcrest-core</artifactId>
      <groupId>org.hamcrest</groupId>
    </exclusion>
  </exclusions>
</dependency>

What am i doing wrong?

Thanks

Stefano

Was it helpful?

Solution

You must specify type arguments for the methods hasItems and hasSize. Automatic type inference by the compiler doesn't work in your case. To specify the type arguments, you must not use the static imported methods, but qualify them with their declaring class - Matchers.<Song>hasItems(i1,i2,i3) for example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top