我需要类似的东西:
assertThat({throw new Exception("hey!")}).throws(Exception).hasMessage("hey!");
在Java中我使用了标准方法,但对于带闭包的语言来说太过分了:
try{
throw new Exception("hey!");
fail("not thrown");
}catch (Exception e){
assertThat(e).hasMessage("hey!");
}
非常欢迎使用JUnit,Hamcrest,Fest或Mockito等框架。
答案 0 :(得分:1)
我在@OliCharlesworth的帮助下找到的最接近的匹配是来自FestAssert库的assertThat
和来自GroovyAssert的shouldFail
:
assertThat(shouldFail(Exception, {throw new Exception("hey")})).hasMessage("hey")