what's wrong with this. I have junit 4
Asked
Active
Viewed 2,330 times
-2
-
1What's the question/problem? – Maroun May 14 '13 at 20:46
-
How do I catch the two exceptions correctly in junit, because right now they are being caught and my tests are failing. – letter Q May 14 '13 at 20:48
-
Show us more of your test, and the stack trace of the exception. The snippet you gave doesn't even invoke the constructor. Tell us what you're trying to test. – JB Nizet May 14 '13 at 20:51
-
1Why did you change the question around so much? – Makoto May 15 '13 at 02:51
2 Answers
5
You can declare on the @Test
annotation that, for the test to pass, it must throw these exceptions:
@Test(expected = NullPointerException.class)
public void testSynapseOne() {
// test
}
@Test(expected = IllegalStateException.class)
public void testSynapseTwo() {
// test
}
Of course, you have to be sure you're testing the right thing - currently, your tests don't make use of the constructor, which is the critical piece you want to test.
Oh - you don't want to have your tests extend TestCase
unless you need compatibility with JUnit3.x.
2
You can use the annotation @Test(expected = TheClassException.class)
to write a test which is supposed to throws the exception of class TheClassException

Alexis C.
- 91,686
- 21
- 171
- 177