Junit Exceptional Test

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

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

One of the tests fails even if an IllegalArgumentException is thrown and the other is. Any idea what is missing? I modified a test that does not match the following and passes.

 public void testSomething(){ try{ ............ //line that throws exception fail(); }catch(IllegalArgumentException e) { } } 
+4
source share
1 answer

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

Perhaps this is so that the test does not work at all (and therefore makes you think that it passes)?

+7
source

Source: https://habr.com/ru/post/1304353/


All Articles