How to get Junit 4 to ignore a Base Test Class?
Asked Answered
U

6

78

I have a base class for many tests that has some helper methods they all need.

It does not by itself have any tests on it, but JUnit (in eclipse) is invoking the test runner on it and complaining that there are no methods to test.

How can I make it ignore this class?

I know I could add a dummyTest method that would solve the problem, but it would also appear for all the children classes.

Suggestions?

Undergarment answered 20/1, 2009 at 15:0 Comment(3)
It is and still gets runUndergarment
See similar question: #672966Corley
i just came to know that incase we write a testcase then there should be atleast 1 @Test method inside the testcase and its mandotory. Other wise it would give us initialization error. Is it true?Carolynncarolynne
D
107

Use to @Ignore annotation. It also works on classes. See this one:

@Ignore public class IgnoreMe {
                        @Test public void test1() { ... }
                        @Test public void test2() { ... }
                }

Also, you can annotate a class containing test methods with @Ignore and none of the containing tests will be executed.

Source: JUnit JavaDoc

Devalue answered 20/1, 2009 at 15:12 Comment(4)
I agree that this should work, so +1. Except with netbeans 6.2 and jUnit 4.5 @ignore is listed as only being valid for Methods.Undergarment
Looking it up and Libary's called 4.5 but the jar is junit4-1.jar weird. Accepted your answer. Thanks.Undergarment
Can you point me to valid reference where it says @Ignore is valid for methods only from junit 4.5? I can't seem to find it.Bathroom
...and if I want to ignore test for all methods except one (instead of putting @ignore above each method)?Tjaden
P
34

Just as a note, I'd always recommend giving a reason for the ignore:

@Ignore("This test will prove bug #123 is fixed, once someone fixes it")

I'm hoping the junit xml report formatter, used when running tests from ant, will one day include the ignored count (and the reasons) along with pass, fail, and error.

Pimbley answered 29/1, 2009 at 13:5 Comment(1)
Years passed, but the bug #123 is not fixed yet..)Deejay
C
27

Making the class abstract should be enough for JUnit 4. If it doesn't work, double check which version of JUnit you're using.

This also communicates your intent that this is just a fragment of a test.

It also prevents JUnit from counting the tests in the class as "ignored" (so the final count of ignored tests will be what you expect).

Alternatively, rename the class. Most of the runners use the class name to determine which classes are tests and which are helpers. In Maven with the default settings, you just have to make sure the base class doesn't begin or end with Test and doesn't end with TestCase (http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html)

Centriole answered 26/3, 2013 at 15:27 Comment(4)
The combination of @Ignore and abstract is excellet!Racial
You do not need @Ignore. Abstract is enough (and best in my opinion)Ballottement
With JUnit 4, abstract should be enough.Centriole
Way to go! With @Ignore and without abstract, I had unwanted skipped tests in my final report when building from command line with mvn package.Diatomaceous
G
18

JUnit5 - @Disabled

@Ignore not exit in the future version, If you are using JUnit5, you can use @Disabled from JUnit Jupiter

import org.junit.jupiter.api.Disabled;

You can even use @Disabled with a comment @Disabled("some comment here")

Class

Annotate the class, will disable all the tests in the class :

@Disabled
public class DemoTest { }

@Disabled("some comment here")
public class DemoTest { }

Method

@Disabled
public void whenCaseThenResult() { }

@Disabled("some comment here")
public void whenCaseThenResult() { }
Godthaab answered 10/1, 2020 at 9:59 Comment(1)
You are doing great, too. As always, too!Punkah
H
11

There are three options:

  1. In JUnit 4 or 5 its enough to make the base class abstract. If you use @Igonre attribute it will show it as an ignored test (and will add it to the total count of tests).

  2. You can use the @Ignore annotation. This annotation also works for classes. The @Ignore test annotation is used to ignore particular tests or groups of tests in order to skip the build failure.

    @Ignore("Base class not yet ready") 
    class MyBaseClassTestCases {...}
    
  3. You can change the name of the test class that it will not have the word Test (case sensitive) at the start or the end of the test name/or the word TestCase at the end.

Hackbut answered 26/12, 2017 at 14:14 Comment(2)
Most relevant answer IMO.Plato
abstract is definitively the way to go for a base test classDisappear
A
1

Adding an empty test works.

@Test(expected = Test.None.class)
public void ATest() {}

Beware, adding it without (expected = Test.None.class) will add an "Add at least one assertion" sonar issue.

Assets answered 18/2, 2022 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.