[英]JUnit checked exception test
I have the following test:我有以下测试:
@Test(expected = ParseException.class)
public void parserTest4() {
MeticParser.run("send+me-more=money");
}
Note that this test must not be changed.(Its from a test class given from my University) My problem is that the ParseException
is a checked exception so it must eiter be surrounded with try/catch
or a throws
statement must be added to the methode head.请注意,不得更改此测试。(它来自我的大学提供的测试 class)我的问题是ParseException
是一个检查异常,因此它必须被try/catch
包围,否则必须将throws
语句添加到方法中头。
Using try/catch
JUnit wont see a ParseException
being thrown.使用try/catch
JUnit 不会看到抛出ParseException
。
Using the throws
signatur will result in me changing the test class (not allowed) since it must be given upwards until its being handeld.使用throws
签名将导致我更改测试 class (不允许),因为它必须向上给出直到它被处理。
Is there a workaround of some sort?有某种解决方法吗?
The answer is to add a throws clause with the checked exception.答案是添加带有已检查异常的 throws 子句。 JUnit expects exceptions to be thrown from tests, it catches the exception, saves the stacktrace, and marks the method as failed. JUnit 期望从测试中抛出异常,它捕获异常,保存堆栈跟踪,并将方法标记为失败。
This isn't something to work around.这不是要解决的问题。 If you're not allowed to change it, your instructor should.如果您不允许更改它,您的教练应该这样做。 Show your instructor that the test doesn't compile.向您的讲师表明该测试无法编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.