37

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.

kaya3
  • 47,440
  • 4
  • 68
  • 97
Pops
  • 30,199
  • 37
  • 136
  • 151
  • 2
    wild guess: Look at your import statements and make sure the annotation is `org.junit.Ignore` or whatever it's supposed to be, and not some other annotation from a totally different package which just happens to have the same name. – Tyler Jun 02 '11 at 17:30
  • @Matrix, good thought, but I do have the right import. – Pops Jun 02 '11 at 17:35
  • Do you run JUnit 3 or Junit 4? JUnit 3 will ignore the annotation. To find out, rename the method to do not start with `test` and remove the @Ignore annotation and try again. If the test is not executed means that you are running Junit 3 – Op De Cirkel Jun 02 '11 at 17:40
  • As I stated in the initial and edited versions of the post, I am using JUnit 4. – Pops Jun 02 '11 at 17:42
  • 1
    How are you running the tests - in the IDE, from Maven, Ant, etc? How did you verify that the tests are run with a JUnit 4 runner? – matt b Jun 02 '11 at 17:49
  • @matt, I moved this comment (my reply to you) to the body of the question. – Pops Jun 02 '11 at 17:55

7 Answers7

50
  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).

Craig Otis
  • 31,257
  • 32
  • 136
  • 234
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • I am, and it is; I updated my post while you were writing this answer. Thanks, though! – Pops Jun 02 '11 at 17:40
  • 3
    Argh, this turned out to be right! Someone else modified a superclass to be JUnit 3 while I was working and I didn't know about it. – Pops Jun 02 '11 at 23:42
14

In JUnit5 use @Disabled or @Disabled("Reason text")

Falke Design
  • 10,635
  • 3
  • 15
  • 30
7

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.

compiledweird
  • 918
  • 2
  • 12
  • 30
  • Superb, this helped. It was getting frustrating because the maven builds were ignoring the @ignore and then failed. Thank you! – Andrei May 26 '16 at 07:37
  • One other thing I noticed is that new version of surefire plugin (version >=2.8) works with testXyz that's annotated with @Ignore, so you don't need to rename methods. – barryku Jul 27 '16 at 01:45
2

Are you sure the test classes were recompiled?

It's a quite common problem, that the recompilation fails because there was typo somewhere in the sources (like a missing semicolon), and the IDE does not tell you that compiling failed.

Try deleting the target/test-classes folder.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • or just using Project > Clean – Tyler Jun 02 '11 at 17:52
  • 1
    @MatrixFrog: I kind of dislike `mvn clean`: deleting `target` is always faster than invoking clean. Also if you have a project with many classes and generated code, clean deletes more than appropriate. – Kijewski Jun 02 '11 at 17:59
  • Took me a while to find them (it's someone else's build script and I'm not familiar with it yet) but this doesn't seem to be my issue. – Pops Jun 02 '11 at 19:32
1

In my case I found my IDE was executing the test disregarding the @Ignore annotation. When I ran mvn install (or any other maven phase) the test was skipped and that's what I was actually aiming for (see attached ilustration).

Ilustration of ways of execution

1

Ensuring the test class is also imported fixed the issue for me i.e.

import org.junit.Ignore;
import org.junit.Test;
mtotowamkwe
  • 2,407
  • 2
  • 12
  • 19
0

I think its just @Ignore that will skip the test

JUnit Ignore

RMT
  • 7,040
  • 4
  • 25
  • 37
  • I did try that, but you realize the page you linked to has both, right? "For example: `@Ignore @Test public void something() { ...`" – Pops Jun 02 '11 at 18:14