Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can cause JUnit to disregard @Ignore annotations?

Tags:

java

junit

I just used MyEclipse to automatically generate some JUnit test cases. One of the generated methods looks like this:

@Ignore("Ignored") @Test public void testCreateRevision() {     fail("Not yet implemented"); // TODO } 

I added the @Ignore annotation manually. However, when I run the test, JUnit lists that method, and others like it, under "failures," rather than ignoring them (related: What's the difference between failure and error in JUnit?). And it displays the "Not yet implemented" message instead of the "Ignored" message. Clearly, fail() must be getting called, and therefore, the @Ignore assertion is not working.

What's going on here? Is there a setting I need to enable for this to work?

EDIT :
Things I have considered/tried so far:

  • I am using JUnit 4, so it's not a version problem.
  • I am importing org.junit.Ignore, so it's not a case of the wrong Ignore being used.
  • I have tried using @Ignore alone, @Ignore @Test and @Ignore("message") @Test; all fail.

EDIT 2 :
I created the tests with MyEclipse, via New > Other; Java > JUnit > JUnit Test Case; New JUnit 4 test, and the library in my build path is JUnit 4. I'm building with ant and actually running the case with MyEclipse.

like image 988
Pops Avatar asked Jun 02 '11 17:06

Pops


People also ask

What is correct use of @ignore annotation in JUnit?

A test method annotated with @Ignore will not be executed. If a test class is annotated with @Ignore, then none of its test methods will be executed.

Why is JUnit ignoring test?

Sometimes you may require not to execute a method/code or Test Case because coding is not done fully. For that particular test, JUnit provides @Ignore annotation to skip the test. Ignore all test methods using @Ignore annotation.

Which of the annotation is not available in JUnit?

Explanation: Auto, Table, Identity and Sequence are the ID generating strategies using @GeneratedValue annotation. 7. Which one of the following is not an annotation used by Junit with Junit4? Explanation: @Test, @Before, @BeforeClass, @After, @AfterClass and @Ignores are the annotations used by Junit with Junit4.


2 Answers

  1. Make sure you are importing the right @Ignore. To be sure use @org.junit.Ignore explicitly.

  2. Double check if your test is being executed by JUnit 4, not 3. The easiest way to do this is to either change the test name so it is not prefixed by test (now it shouldn't be executed at all and JUnit 4 does not need this prefix anyway) or examine your test case inheritance hierarchy: it shouldn't extend directly or indirectly from junit.framework.TestCase (Junit 3 requirement).

like image 69
Tomasz Nurkiewicz Avatar answered Sep 28 '22 10:09

Tomasz Nurkiewicz


I had this problem also even though JUnit 3 was not on my classpath. I believe that compatibility mode on Junit 4 picks up on the 'test' prefix in your testname and thus operates as JUnit 3 would rather than picking up the @Ignore. The solution is to rename your test.

like image 22
compiledweird Avatar answered Sep 28 '22 10:09

compiledweird