Question

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) {
 }
} 
Was it helpful?

Solution

Prithis just something I noticed the second test 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)?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top