How do You Unit Test a Custom Assert?
I'm writing my own JUnit Assert? How do I test it?
I know how to feed it something that w开发者_Python百科ill pass and something that will make it fail, but how do I write a JUnit test for those things?
The custom assert will look something like:
public static void assertSomething() {
if (!something()) {
fail("Expected something, but ...");
}
}
How can I catch that fail?
fail()
throws a junit.framework.AssertionFailedError
, which you could catch in a unit test of your assertion method, if you like.
Example:
@Test(expected = AssertionFailedError.class)
public void testMyAssertFails() {
assertSomething("valueThatWillFail");
}
@Test
public void testMyAssertPasses() {
assertSomething("valueThatPasses");
//if you reach this line, no failure was thrown
}
精彩评论