JUnit checked exception test

Viewed 26

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.

Using try/catch JUnit wont see a ParseException being thrown.

Using the throws signatur will result in me changing the test class (not allowed) since it must be given upwards until its being handeld.

Is there a workaround of some sort?

1 Answers

The answer is to add a throws clause with the checked exception. JUnit expects exceptions to be thrown from tests, it catches the exception, saves the stacktrace, and marks the method as failed.

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.

Related