I am trying to figure out why this is happening. My main() method contains some initialization procedures that are vital for application startup and spring boot integration tests are not invoking it.
Can someone pls explain this behavior.
Spring boot test does not need call SpringBootApplication.main() because it scans pacakes for configurations by himself. It tries to mimic the processes that use Spring Boot framework for creating the context. In other words it scans based on package structures, loads external configurations from predefined locations, optionally runs auto-configuration starters and so on. Interesting this is that If you just put @SpringBootTest on your test class, scanning process will be done on packages in up direction
Example: For following project structure
└── com
└── example
└── demo
├── config
│ └── AppConfig.java
├── test
| └── SpringBootAppTest.java
└── DemoApplication.java
Spring Boot test will search for SpringBootConfiguration in:
com.example.demo.testcom.example.democom.examplecomSo when it will find SpringBootApplication (which is SpringBootConfiguration) it is load it's context that scan for other configuration and beans in another direction in sample case com.example.demo.configuration . Therefore all context will be loaded.
If you for example move SpringBootAppTest up to com.example it will fail (because SpringBootApplication can't be found) with following error:
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With