简体   繁体   中英

JUnit 5: Difference between BeforeEachCallback and BeforeTestExecutionCallback

I can't find any ressources explaining what exactly the difference between BeforeEachCallback and BeforeTestExecutionCallback in the JUnit Jupiter extension model is. (I am of course also interested in the "After"-variants)

To my understanding, the following timeline describes what is happening:

BeforeEach - BeforeTestExecution - Actual execution of the test - AfterTestExecution - AfterEach

I suppose that BeforeTestExecution exists so you can execute code after all the BeforeEach callbacks have been worked on but before the actual test execution. However this is still unclear to me, because everyone could just use BeforeTestExecution instead of BeforeEach and the order of execution of these callbacks is random again.

So what is BeforeTestExecution exactly for and what happens if you use this callback in multiple extensions at the same time?

The Javadocs ( here and here ) don't make a clear distinction between them but the JUnit5 docs include the following:

BeforeTestExecutionCallback and AfterTestExecutionCallback define the APIs for Extensions that wish to add behavior that will be executed immediately before and immediately after a test method is executed, respectively. As such, these callbacks are well suited for timing, tracing, and similar use cases. If you need to implement callbacks that are invoked around @BeforeEach and @AfterEach methods, implement BeforeEachCallback and AfterEachCallback instead.

So, if you want to wrap just the test execution without any of the setup then use BeforeTestExecutionCallback . The docs go on to suggest timing and logging test execution as possible use cases for BeforeTestExecutionCallback .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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