简体   繁体   English

@ Before / @ BeforeEach继承行为更改JUnit4 / JUnit5

[英]@Before/@BeforeEach inheritance behaviour change JUnit4 / JUnit5

When migrating from JUnit4 to JUnit5 I found a change in the behaviour of JUnit4 and JUnit5 and wanted to check if the change was a bug in JUnit4 or in JUnit5 and how to do it correctly. 从JUnit4迁移到JUnit5时,我发现JUnit4和JUnit5的行为发生了变化,并想检查该变化是否是JUnit4或JUnit5中的错误,以及如何正确执行。

Lets assume the following structure: 让我们假设以下结构:

One base class 一个基层

public class BaseTestClass {

    @Before
    public void setUp(){
        System.out.println("Base Test Class");
    }
}

Another class that inherits from this base class 从该基类继承的另一个类

public class InheritsFromBase extends BaseTestClass {

    @Override
    public void setUp() {
        System.out.println("I inherit from base");

        super.setUp();
    }
}

And an actual test class 和一个实际的测试班

public class ActualTests extends InheritsFromBase {

    @Test
    public void myTest(){
        Assert.assertTrue(true);
    }
}

If I run myTest() in JUnit 4 then the setUp() method of both, BaseTestClass and InheritsFromBase is called. 如果我在JUnit 4中运行myTest()BaseTestClass调用BaseTestClassInheritsFromBasesetUp()方法。

After migrating this code to JUnit5, the setUp() methods are not called anymore. 在将此代码迁移到JUnit5之后,不再调用setUp()方法。 I had to manually add the @BeforeEach annotation on InheritsFromBase . 我必须在InheritsFromBase上手动添加@BeforeEach批注。

Resulting in the following classes: 产生以下类别:

public class BaseTestClass {

    @BeforeEach
    public void setUp(){
        System.out.println("Base Test Class");
    }
}

public class InheritsFromBase extends BaseTestClass {

    @Override
    @BeforeEach
    public void setUp() {
        System.out.println("I inherit from base");

        super.setUp();
    }
}

public class ActualTests extends InheritsFromBase {

    @Test
    public void myTest(){
        Assertions.assertTrue(true);
    }
}

So my question: Was the behaviour in JUnit4 correct or in JUnit5? 所以我的问题是:JUnit4中的行为正确还是JUnit5中的行为正确?

The actual behavior for JUnit 5 is expected as the @BeforeEach javadoc states : 预期JUnit 5的实际行为是@BeforeEach javadoc指出的:

Inheritance 遗产

@BeforeEach methods are inherited from superclasses as long as they are not overridden. @BeforeEach方法是从超类继承的,只要它们不被覆盖即可。

You override the setup() method that contains @BeforeEach in the InheritsFromBase class. 您重写InheritsFromBase类中包含@BeforeEachsetup()方法。
So it is not inherited any longer. 因此,它不再继承。

For JUnit 4, the @Before javadoc doesn't state any particularity and inheritance abilities. 对于JUnit 4, @Before javadoc没有声明任何特殊性和继承能力。
So you should consider the actual behavior as "normal" but no documented. 因此,您应将实际行为视为“正常”,但没有记录。

To get the same behavior with JUnit 5, you should do the reverse what you do with JUni4 : remove @BeforeEach in the super class and add it only in the subclass. 为了获得与JUnit 5相同的行为,您应该执行与JUni4相反的操作:删除超类中的@BeforeEach并将其仅添加到子类中。

public class BaseTestClass {

    public void setUp() {
        System.out.println("Base Test Class");
    }
}


public class InheritsFromBase extends BaseTestClass {

    @Override
    @BeforeEach
    public void setUp() {
        System.out.println("I inherit from base");
        super.setUp();
    }
}

As I execute the tests, it produces as output : 当我执行测试时,它生成为输出:

I inherit from base 我从基地继承

Base Test Class 基础测试课

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM