Question

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?

Was it helpful?

Solution

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()));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top