tags:

views:

198

answers:

1

I have two tests to check the expected exception throw. I am using Junit 4 and has following syntax.

@Test(expected=IllegalArgumentException.class)
public void testSomething(){
..........
}

One of the tests fail even though IllegalArgumentException is thrown and the other passes. Any idea whats missing?? I modified the test which is failing to following and it passes.

public void testSomething(){
 try{
  ............ //line that throws exception
  fail();
 }catch(IllegalArgumentException e) {
 }
} 
+2  A: 

Prithis just something I noticed the second test that you have does not have @Test annotation at all. JUNIT4 does not run tests that are not annotated even if the method names starts with test*** (unless of course you actually extend the TestCase class in which case it behaves like a JUNIT3.x testcase)

Perhaps that is the case that the test is not running at all (and hence makes you think that it passes)?

Calm Storm
Calm well spotted. Yes I did extend TestCase and did not have @test. I was trying to mix junit 3 and 4. Its working now. I had to import static org.junit.Assert.*; not extend testCase and add @test
Prithis