org.hamcrest.Matchers.any 在 java 8 中不工作

bra*_*uka 1 java junit hamcrest mockito

Hamcrest Matchers any() 在 Java 8 中不起作用。

when(simpleJdbcCall.execute(Matchers.any(SqlParameterSource.class))).thenReturn(outputParameters);
Run Code Online (Sandbox Code Playgroud)

any() 仅适用于已弃用的 org.mockito.Matchers 。

在Java 8中还有其他方法使用这个方法吗?

Oli*_*ire 5

使用 Mockito 的any(Class),而不是 Hamcrest 的

when(simpleJdbcCall.execute(Mockito.any(SqlParameterSource.class))).thenReturn(outputParameters);
Run Code Online (Sandbox Code Playgroud)

您正在尝试让 Mockito 使用 Hamcrest 的方法。这是行不通的。因此,请将您的呼叫从 更改Matchers.any(SqlParameterSource.class)Mockito.any(SqlParameterSource.class)