Prevent Application / CommandLineRunner classes from executing during JUnit testing
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Spring Boot applications, the CommandLineRunner and ApplicationRunner interfaces are used to execute specific code after the application context is loaded. While these are useful for initializing data or performing any startup logic, they can be intrusive during JUnit testing as they might add overhead or complicate test scenarios. In this article, we will explore strategies to prevent these classes from executing during JUnit testing.
Understanding CommandLineRunner and ApplicationRunner
Before delving into the solutions, let's briefly explore what these interfaces do:
- CommandLineRunner: This is a functional interface in Spring Boot used to execute code after application startup. It provides a
runmethod that accepts an array ofStringarguments. - ApplicationRunner: Similar to
CommandLineRunner, but it provides access to theApplicationArgumentsinterface, which gives a richer access to application arguments, including options and non-option arguments.
Example of CommandLineRunner
Why Prevent Execution During Testing?
When executing unit tests, especially those focused on specific application logic, running startup code can lead to undesired side effects such as modifying the state, making external HTTP requests, or simply making the test slower. Therefore, it's often desirable to prevent such execution during tests.
Strategies To Prevent Execution
1. Profile Based Exclusion
Use Spring Profiles to conditionally include or exclude components. By default, exclude the CommandLineRunner component in a test profile.
By activating a test profile during tests, the StartupRunner will be excluded.
2. Conditional Bean
Utilize conditional beans to selectively load beans based on the presence of specific properties or profiles.
You can then disable it in your application-test.properties file:
3. Mock Configuration in Test
Create a separate test configuration class that mocks or removes these components when running tests.
Include this configuration in your tests:
Summary of Key Strategies
| Strategy | Description | Pros | Cons |
| Profile Based Exclusion | Use a test profile to exclude startup logic | Simple to implement | Requires profile management |
| Conditional Bean | Conditional loading based on property configuration | Flexible | Adds complexity |
| Mock Configuration in Test | Provide alternate mock implementations during tests | Fine-grained control | Requires additional boilerplate |
Additional Considerations
- Efficiency: Using profiles and conditions is efficient and adds minimal overhead to your test setup.
- Complexity Management: Too many conditions may complicate configuration management, so balance between configurability and simplicity is key.
- Documentation: Document your configurations to ensure developers understand test configurations and bean loading logic.
Incorporating these strategies ensures that your test suites remain focused, reliable, and unaffected by non-essential startup logic. By doing so, you can achieve better isolation, making your tests faster and more meaningful.
Related reading
- Prevent @RabbitListener in spring-rabbit from trying to connect to server during integration test
- Prevent Spring Boot application closing until all current requests are finished
- Prevent Spring Boot from registering a servlet filter
- Prevent stack trace logging for custom exception in Spring Boot application
- Printing test execution times and pinning down slow tests with py.test
- Protractor- Generic wait for URL to change
- Print all the Spring beans that are loaded - Spring Boot
- Print an integer in binary format in Java

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.