【发布时间】:2015-02-03 15:12:17
【问题描述】:
在第一次和第二次调用时抛出的方法:
public void foo() throws Exception
测试:
@test
public void testFooThrowsAtFirstAndSecondTime(){
boolean thrown;
try {
foo();
} catch (Exception e) {
thrown = true;
}
assertTrue(thrown);
thrown = false;
try {
foo();
} catch (Exception e) {
thrown = true;
}
assertTrue(thrown);
foo();
}
您能帮我找到更好的解决方案吗? 使用 Mockito 来获得更好的解决方案也是可以接受的。
我的意思是,如果我可以在我的测试中避免 try/catch 甚至多次 try/catch。在其他语言或 jAssert 中,我认为即使在春天也有这样的陈述:
assertThrows(method(..)) //PseudoCode
我认为 Mockito 或 JUnit 4.x 也有类似的东西。
我知道
@Test(expected=Exception)
但这只有在我期望一次投掷并且在那之后测试结束时才可以接受。
【问题讨论】:
-
有什么问题?问题是什么?您究竟需要什么解决方案?
-
在 jAssert 或其他语言中,有一些类似 assertThrow 的东西,我会发现它除了更具可读性之外,在某种程度上也是一种更正确的方法。
标签: java junit exception-handling mockito assert