Java 使用hamcrest的Assert.fail等价物
我将JUnit用于Assert.fail,但我不知道它的等价物是什么。有人知道吗?matcherasert类有以下方法:Java 使用hamcrest的Assert.fail等价物,java,unit-testing,junit,hamcrest,Java,Unit Testing,Junit,Hamcrest,我将JUnit用于Assert.fail,但我不知道它的等价物是什么。有人知道吗?matcherasert类有以下方法: public static void assertThat(String reason, boolean assertion) { if (!assertion) { throw new AssertionError(reason); } } 因此,当调用时,它将是最接近的东西: MatcherAssert.assertThat("Fail
public static void assertThat(String reason, boolean assertion) {
if (!assertion) {
throw new AssertionError(reason);
}
}
因此,当调用时,它将是最接近的东西:
MatcherAssert.assertThat("Fail here", false);
根据测试的结构,我发现使用not(anything())匹配器更自然
@Test(expected = MyException.class)
public void runMyTestHere() {
...
MyObj result = myService.getThing(id);
assertThat("Exception should have been thrown.", result, is(not(anything())));
}