2021-01-08

JUnit5 @RunWith

 

In JUnit 5, the @RunWith annotation has been replaced by the more powerful @ExtendWith annotation.




.

4. Migrating from a JUnit4-Based Runner 

Let's now migrate a test that uses a JUnit4-based runner to JUnit5.

We're going to use a Spring test as an example:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class GreetingsSpringTest {
    // ...
}

If we want to migrate this test to JUnit5 we need to replace the @RunWith annotation with the new @ExtendWith:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class GreetingsSpringTest {
    // ...
}

The SpringExtension class is provided by Spring 5 and integrates the Spring TestContext Framework into JUnit 5. The @ExtendWith annotation accepts any class that implements the Extension interface.

.

No comments:

Google Referrals