Can I exclude an individual test from @BeforeEach in JUnit5?

蹲街弑〆低调 提交于 2020-01-06 08:12:50

问题


Is it possible to exclude an individual test from @BeforeEach in JUnit5?

Kindly guide me for this.

Thank you


回答1:


You can move the tests that require the before-each behaviour into an inner ˋ@Nested´ subclass and put the before-each method there.




回答2:


You could use TestInfo and write this condition by inspecting the test name:

@BeforeEach 
void init(TestInfo info) {
  if (info.getDisplayName().equals("mySpecialTestName") {
    return; // skip @BeforeEach in mySpecialTestName test
  }
}

but it would be cleaner to move the tests that don't need @BeforeEach to a separate class.



来源:https://stackoverflow.com/questions/58772186/can-i-exclude-an-individual-test-from-beforeeach-in-junit5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!