Frage

Am I missing something here?

@Test
public void testAnything(){
    Random random = new Random();
    assertThat(random.nextInt(), is(equalTo(anything())));
}

This doesn't compile. Eclipse complains with "The method assertThat(T, Matcher) in the type MatcherAssert is not applicable for the arguments (int, Matcher>)"

Was there something I missed for the use of anything()? I have used other Hamcrest methods in the past... so what is different about this?

War es hilfreich?

Lösung

That's not how equalTo works. It calls Object#equals(Object) internally and would have to pass anything(). That doesn't make sense. Just omit it and it works:

Random random = new Random();
assertThat(random.nextInt(), is(anything()));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top