Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of multiple extensions in JUnit 5

If I use more than one extension with JUnit 5, whats the order? Ideally the order int the @ExtendsWith annotation is respected, but I could not find any documentation about that.

Example:

@ExtendWith({SpringExtension.class, InitH2.class})
public class VmRepositoryIntegrationTest {
  // Test implemenations
}

So in this example I need Spring to set up the DB connection before I cann initialize the DB.

like image 738
BetaRide Avatar asked May 23 '19 04:05

BetaRide


1 Answers

From §5.2.1 of the JUnit 5 User Guide:

...

Multiple extensions can be registered together like this:

@ExtendWith({ DatabaseExtension.class, WebServerExtension.class })
class MyFirstTests {
    // ...
}

As an alternative, multiple extensions can be registered separately like this:

@ExtendWith(DatabaseExtension.class)
@ExtendWith(WebServerExtension.class)
class MySecondTests {
    // ...
}

Extension Registration Order

Extensions registered declaratively via @ExtendWith will be executed in the order in which they are declared in the source code. For example, the execution of tests in both MyFirstTests and MySecondTests will be extended by the DatabaseExtension and WebServerExtension, in exactly that order.

like image 166
Slaw Avatar answered Oct 25 '22 05:10

Slaw