With JUnit 4 it is pretty simple to test if an exception has been properly thrown because you can use annotations:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import static org.junit.Assert.fail; import org.junit.*; public class MyTestClass { @Test(expected = NoMatchFoundException.class) public void testSomething() throws NoMatchFoundException { // Your critical code here! fail(); } } |
If you are using JUnit 3 then it is not that easy because you don’t have annotations. That’s why you have to catch exceptions in the test code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import junit.framework.TestCase; public class MyTestClass extends TestCase { public void testSomething() throws NoMatchFoundException { try { // Your critical code here! } catch (NoMatchFoundException ex) {} } } |