
1. Overview
Testing is critical for any application, whether a unit test or an integration test. SpringRunner and SpringBootTest classes form the foundation to run integration tests.
In this tutorial, we’ll learn about both. We’ll learn how to use them in our code and learn about their similarities and differences.
2. SpringRunner
SpringRunner is an alias for the class SpringJUnit4ClassRunner and works with JUnit4-based test classes. It loads Spring TestContext, through which spring beans and configurations become available to use along with JUnit annotations. We’ll require JUnit 4.12 or higher to use this.
To use this in code, annotate the test class with @RunWith(SpringRunner.class):
@RunWith(SpringRunner.class)
public class SampleIntegrationTest {
@Test
public void test() {
//
}
}
3. SpringBootTest
SpringBootTest is an alternative to SpringRunner and works with JUnit5. It’s also used to run integration tests and load Spring TestContext.
It is very rich and provides many configurations through its annotation arguments. It supports various web environment modes such as MOCK, RANDOM_PORT, DEFINED_PORT, and NONE. We can pass application properties through its annotation that is injected in the spring environment before the test runs:
@SpringBootTest(
properties = {"user.name=test_user"},
webEnvironment = MOCK)
public class SampleIntegrationTest {
@Test
public void test() {
//
}
}
The annotation @SpringBootTest is required at the class level to run the integration test.
4. Comparison Between SpringRunner and SpringBootTest
In this table, we’ll compare both the classes with their pros and cons.
| SpringRunner | SpringBootTest |
|---|---|
| Use to run integration tests and load Spring TestContext | Use to run integration tests and load Spring TestContext |
| JUnit annotations are also available to use | JUnit annotations are also available to use |
| Needs JUnit4.12 or higher | Needs JUnit5 or higher |
| Not a rich API with respect to configuration | Offer a rich API to configure test configuration |
| Not recommended | Recommended as it supports new features and is simple to use |
5. Conclusion
In this article, we learned about SpringRunner and SpringBootTest. We’ve learned how to use them. We’ve also compared them and learned their differences and similarities.
We should use SpringBootTest as it supports the latest JUnit, but whenever there is a requirement to use JUnit 4, SpringRunner is the option.
Commercials Cooperation Advertisements:
(1) IT Teacher IT Freelance

立刻註冊及報名電腦補習課程吧!
电子计算机 -教育 -IT 電腦班” ( IT電腦補習 ) 提供一個方便的电子计算机 教育平台, 為大家配對信息技术, 電腦 老師, IT freelance 和 programming expert. 讓大家方便地就能找到合適的電腦補習, 電腦班, 家教, 私人老師.
We are a education and information platform which you can find a IT private tutorial teacher or freelance.
Also we provide different information about information technology, Computer, programming, mobile, Android, apple, game, movie, anime, animation…
(2) ITSec
www.ITSeceu.uk
Secure Your Computers from Cyber Threats and mitigate risks with professional services to defend Hackers.
ITSec provide IT Security and Compliance Services, including IT Compliance Services, Risk Assessment, IT Audit, Security Assessment and Audit, ISO 27001 Consulting and Certification, GDPR Compliance Services, Privacy Impact Assessment (PIA), Penetration test, Ethical Hacking, Vulnerabilities scan, IT Consulting, Data Privacy Consulting, Data Protection Services, Information Security Consulting, Cyber Security Consulting, Network Security Audit, Security Awareness Training.
Contact us right away.
Email (Prefer using email to contact us):
SalesExecutive@ITSec.vip