Master the most popular testing framework for Java, through the Learn JUnit course:
Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.
Get started with mocking and improve your application tests using our Mockito guide:
Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.
Get started with understanding multi-threaded applications with our Java Concurrency guide:
Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:
Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
But these can also be overused and fall into some common pitfalls.
To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:
Get started with Spring and Spring Boot, through the Learn Spring course:
>> LEARN SPRINGExplore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:
Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.
I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.
You can explore the course here:
Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.
Get started with Spring Data JPA through the guided reference course:
Refactor Java code safely β and automatically β with OpenRewrite.
Refactoring big codebases by hand is slow, risky, and easy to put off. Thatβs where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.
Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions β one for newcomers and one for experienced users. Youβll see how recipes work, how to apply them across projects, and how to modernize code with confidence.
Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.
1. Introduction
In this quick tutorial, weβll discuss the use of the @RunWith annotation in the JUnit 5 framework.
In JUnit 5, the @RunWith annotation has been replaced by the more powerful @ExtendWith annotation.
However, the @RunWith annotation can still be used in JUnit 5 for the sake of backward compatibility.
2. Running Tests With a JUnit 4-Based Runner
We can run JUnit 5 tests with any older JUnit environment using the @RunWith annotation.
Letβs look at an example of running tests in an Eclipse version that only supports JUnit 4.
First, letβs create the class weβre going to test:
public class Greetings {
public static String sayHello() {
return "Hello";
}
}
Then weβll create this plain JUnit 5 test:
public class GreetingsUnitTest {
@Test
void whenCallingSayHello_thenReturnHello() {
assertTrue("Hello".equals(Greetings.sayHello()));
}
}
Finally, letβs add this annotation so weβre able to run the test:
@RunWith(JUnitPlatform.class)
public class GreetingsUnitTest {
// ...
}
The JUnitPlatform class is a JUnit 4 based runner that lets us run JUnit 4 tests on the JUnit Platform.
Letβs keep in mind that JUnit 4 doesnβt support all the features of the new JUnit Platform, so this runner has limited functionality.
If we check the results of the test in Eclipse, we can see that a JUnit 4 runner was used:
π junit4 test3. Running Tests in a JUnit 5 Environment
Now letβs run the same test in an Eclipse version that supports JUnit 5. In this case, we donβt need the @RunWith annotation anymore, and we can write the test without a runner:
public class GreetingsUnitTest {
@Test
void whenCallingSayHello_thenReturnHello() {
assertTrue("Hello".equals(Greetings.sayHello()));
}
}
The test results show that weβre now using the JUnit 5 runner:
π junit5 test4. Migrating From a JUnit 4-Based Runner
Now letβs migrate a test that uses a JUnit 4 based runner to JUnit 5.
Weβre going to use a Spring test as an example:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class GreetingsSpringUnitTest {
// ...
}
If we want to migrate this test to JUnit 5, we need to replace the @RunWith annotation with the new @ExtendWith:
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class GreetingsSpringUnitTest {
// ...
}
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.
5. Conclusion
In this brief article, we covered the use of JUnit 4βs @RunWith annotation in the JUnit 5 framework.
