JUnit 4 on How to Ignore a Base Test Class – JUnit 4关于如何忽略一个基础测试类的问题

最后修改: 2021年 9月 29日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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.

本教程将讨论在JUnit 4中跳过运行基测试类的测试的可能解决方案。在本教程中,基类只有辅助方法,而子类将扩展它并运行实际测试

2. Bypass Base Test Class

2.绕过基础测试类

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

让我们假设我们有一个带有一些辅助方法的BaseUnitTest类。

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.

如果我们运行测试,无论是用IDE还是Maven构建,我们可能会得到一个错误,告诉我们在BaseUnitTest中没有可运行的测试方法。我们不想在这个类中运行测试,所以我们正在寻找一种方法来避免这个错误。

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.

我们将看一下三种不同的可能性。如果用IDE运行测试,我们可能会得到不同的结果,这取决于我们IDE的插件和我们如何配置它来运行JUnit测试。

2.1. Rename Class

2.1.重命名类

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.

我们可以将我们的类重命名为一个构建约定将排除在测试运行之外的名字。例如,如果我们使用Maven,我们可以检查Maven Surefire Plugin的默认设置。

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

我们可以把名字从BaseUnitTest改为BaseUnitTestHelper或类似的

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

2.2. Ignore

2.2.忽略不计

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:

第二个选择是使用JUnit @Ignore注解暂时禁用测试。我们可以在类的层面上添加它来禁用一个类中的所有测试

@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:

如果用Maven运行,我们会看到如下输出。

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.

自JUnit 5以来,@Disabled注解取代了@Ignore

2.3. Make the Base Class abstract

2.3.使基类成为抽象的

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.

也许最好的方法是制作一个基础测试类抽象。抽象将需要一个具体的类来扩展它。这就是为什么JUnit在任何情况下都不会认为它是一个测试实例。

Let’s make our BaseUnitTest class abstract:

让我们让我们的BaseUnitTest类成为抽象的。

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

3. Conclusion

3.总结

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 4中排除基测试类运行测试的例子。最好的方法是创建抽象类。

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.

JUnit的@Ignore注释也被广泛使用,但被认为是一种不好的做法。通过忽略测试,我们可能会忘记它们以及忽略它们的原因

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

一如既往,本文介绍的代码可以在GitHub上获得