YogevSitton
YogevSitton

Reputation: 10108

Check that exception is not being thrown with JUnit

I want to use JUnit to make sure not exception is being thrown during the execution of my program. How can I do that?

Upvotes: 0

Views: 226

Answers (3)

Vincent Jia
Vincent Jia

Reputation: 612

I'm assuming you're trying to create a wonderful test case, to make sure the specific exception won't get thrown in production mode. A good way is ,in the Sprint Testing and Integration Testing phrases, try to list all potential use cases and ask QA's help. For the cases, use AssertTrue is enough to get all them passed.

Upvotes: -1

mthmulders
mthmulders

Reputation: 9705

If you use JUnit 4, your test method is annotated using @Test. As long as you do not specify an expected attribute, any Exception will cause that test to fail. See the JUnit FAQ for details.

Upvotes: 5

Lee White
Lee White

Reputation: 3709

JUnit will say that your function did not do as expected (like when an AssertTrue returns fals, for instance) if it throws an unhandled exception. So, just don't catch it in JUnit and you should be fine.

Upvotes: 4

Related Questions