I have the following test case in eclipse, using JUnit 4 which is refusing to pass. What could be wrong?
@Test(expected = IllegalArgumentException.class) public void testIAE() { throw new IllegalArgumentException(); }
This exact testcase came about when trying to test my own code with the expected tag didn't work. I wanted to see if JUnit would pass the most basic test. It didn't.
I've also tested with custom exceptions as expected without luck.
Screenshot:
JUnit provides the facility to trace the exception and also to check whether the code is throwing expected exception or not. Junit4 provides an easy and readable way for exception testing, you can use. Optional parameter (expected) of @test annotation and. To trace the information ,”fail()” can be used.
When using JUnit 4, we can simply use the expected attribute of the @Test annotation to declare that we expect an exception to be thrown anywhere in the annotated test method. In this example, we've declared that we're expecting our test code to result in a NullPointerException.
JUnit provides an option of tracing the exception handling of code. You can test whether the code throws a desired exception or not. The expected parameter is used along with @Test annotation. Let us see @Test(expected) in action.
The problem is that your AnnounceThreadTest extends TestCase. Because it extends TestCase, the JUnit Runner is treating it as a JUnit 3.8 test, and the test is running because it starts with the word test, hiding the fact that the @Test annotiation is in fact not being used at all.
To fix this, remove the "extends TestCase" from the class definition.
Instead of removing extends TestCase , you can add this to run your test case with Junit4 which supports annotation.
@RunWith(JUnit4.class)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With