Java – how do you unit test custom assertions?

Am I writing my own JUnit asset? How do I test?

I know how to provide it with something that will pass and make it fail, but how to write JUnit tests for these things?

The custom assertion will look like:

public static void assertSomething() {
    if (!something()) {
        fail("Expected something,but ...");
    }
}

How can I catch that failure?

Solution

Fail() throws a JUnit framework. Assertionfailederror, if you like, you can capture it in the unit test of the assertion method

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
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>