Search code examples
javajunitmockitohamcrest

org.hamcrest.Matchers.any not working in java 8


Hamcrest Matchers any() is not working in Java 8.

when(simpleJdbcCall.execute(Matchers.any(SqlParameterSource.class))).thenReturn(outputParameters);

any() is only working with org.mockito.Matchers which is deprecated.

Is there another way to use this method in Java 8?


Solution

  • Use Mockito's any(Class), not Hamcrest's

    when(simpleJdbcCall.execute(Mockito.any(SqlParameterSource.class))).thenReturn(outputParameters);
    

    You're trying to make Mockito work with Hamcrest's method. It won't work. So change your call from Matchers.any(SqlParameterSource.class) to Mockito.any(SqlParameterSource.class).