我刚刚意识到(在将遗留代码从 JUnit 4 迁移到 JUnit 5 时)我们的一些测试方法没有执行,因为它们没有 @Test
注释。他们没有它,因为它们覆盖了抽象父类(super class)(存在注释的地方)的方法。
我可以通过向每个方法添加 @Test
轻松解决此问题。但我想知道这是否是预期的行为。它从 JUnit 4 更改为 5,但我在 official JUnit5 User Guide 中找不到任何相关信息或其他任何地方。
根据 this question , 注解通常不被继承。但似乎这是在新的 JUnit 版本中有意更改的。 (或者我错过了什么?)
抽象测试类
import org.junit.jupiter.api.Test;
abstract class AbstractJUnit5Test {
@Test
void generalTest() {
System.out.println("This is a test in the abstract class");
}
@Test
abstract void concreteTest();
}
具体测试类
import org.junit.jupiter.api.Test;
class ConcreteJUnt5Test extends AbstractJUnit5Test {
// only gets executed with an additional @Test annotation:
@Override
void concreteTest() {
System.out.println("This is from the concrete test method.");
}
}
最佳答案
这是 JUnit 4 和 JUnit Jupiter 之间无意间的区别。
详见 https://github.com/junit-team/junit5/issues/960
编辑:经过进一步调查,JUnit 4 的这种(方便的)行为似乎实际上是无意的。在 https://github.com/junit-team/junit5/issues/960#issuecomment-316114648 查看 Sam 的最新评论
关于java - 为什么 JUnit 5 测试不从抽象类继承 @Test 注解?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45162254/