Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

This tutorial will discuss possible solutions to skip running tests from base test classes in JUnit 4. For purposes of this tutorial, a base class has only helper methods, while children classes will extend it and run actual tests.

2. Bypass Base Test Class

Let's assume we have a BaseUnitTest class with some helper methods:

public class BaseUnitTest {
    public void helperMethod() {
        // ...
    }
}

Now, let's extend it with a class that has tests:

public class ExtendedBaseUnitTest extends BaseUnitTest {
    @Test
    public void whenDoTest_thenAssert() {
        // ...        
    }
}

If we run tests, either with an IDE or a Maven build, we might get an error telling us about no runnable test methods in BaseUnitTest. We don't want to run tests in this class, so we're looking for a way to avoid this error.

We'll take a look at three different possibilities. If running tests with an IDE, we might get different results, depending on our IDE's plugin and how we configure it to run JUnit tests.

2.1. Rename Class

We can rename our class to a name that the build convention will exclude from running as a test. For example, if we're using Maven, we can check the defaults of the Maven Surefire Plugin.

We could change the name from BaseUnitTest to BaseUnitTestHelper or similar:

public class BaseUnitTestHelper {
    public void helperMethod() {
        // ...
    }
}

2.2. Ignore

A second option would be to disable tests temporarily using the JUnit @Ignore annotation. We can add it at the class level to disable all tests in a class:

@Ignore("Class not ready for tests")
public class IgnoreClassUnitTest {
    @Test
    public void whenDoTest_thenAssert() {
        // ...
    }
}

Likewise, we can add it at the method level, in case we still need to run other tests within the class but only exclude one or a few:

public class IgnoreMethodTest {
    @Ignore("This method not ready yet")
    @Test
    public void whenMethodIsIgnored_thenTestsDoNotRun() {
        // ...
    }
}

If running with Maven, we will see output like:

Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.041 s - in com.baeldung.IgnoreMethodTest

The @Disabled annotation replaces @Ignore since JUnit 5.

2.3. Make the Base Class abstract

Possibly the best approach is to make a base test class abstract. Abstraction will require a concrete class to extend it. That's why JUnit will not consider it a test instance in any case.

Let's make our BaseUnitTest class abstract:

public abstract class BaseUnitTest {
    public void helperMethod() {
        // ...
    }
}

3. Conclusion

In this article, we've seen some examples of excluding a base test class from running tests in JUnit 4. The best approach is to create abstract classes.

JUnit's @Ignore annotation is also widely used but is considered a bad practice. By ignoring tests, we might forget about them and the reason for their ignoring.

As always, the code presented in this article is available over on GitHub.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Junit footer banner
guest
0 Comments
Inline Feedbacks
View all comments