Question

I know that a part of my program will fail when I try and test it and I wan't the test to pass when it fails. So I was wondering if it is possible to assert a failure?

Was it helpful?

Solution

If you're using Junit 4, use the expected behavior:

@Test(expected=SomeException.class)
public void testThings() {
    // do stuff
}

If you're using Junit 3, you'll have to catch it

public void test() {
    try {
        // do stuff
        fail("Expected SomeException");
    } catch (SomeException e) {
    }
}

OTHER TIPS

The JUnit wiki lists different ways of exception testing: https://github.com/junit-team/junit/wiki/Exception-testing

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top