JUnit如何assert exception

走着路睡觉
  • java
小于 1 分钟

import static org.junit.jupiter.api.Assertions.assertThrows;

@Test
void exceptionTesting() {
    MyException thrown = assertThrows(
           MyException.class,
           () -> myObject.doThing(),
           "Expected doThing() to throw, but it didn't"
    );

    assertTrue(thrown.getMessage().contains("Stuff"));
}
上次编辑于:
贡献者: zhaojingbo
Loading...