I'm working on multi-module maven project using Spring Boot 2.4.0. I have written integration tests for a module. The test class looks similar to this.
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringApplicationClassWithMainMethod.class)
public class XYZServiceIT {
@Test
public void test1() {...}
@Test
public void test2() {...}
}
To run the SpringApplicationClassWithMainMethod.class i.e., in order to load the Application context I need few environment variables that I set in eclipse. So, in order to run the above integration test while loading the SpringApplicationClassWithMainMethod.class I need those environment variables before the Application context is loaded.
Trial-1: I have tried using @TestPropertySource(properties = {"key1=val1", "key2=val2"}) annotation, that didn't work.
Trial-2: I have also tried the static block to set environment variables which didnt work.
Trial-3: I also tried using the @ContextConfiguration with a ApplicationContextInitializer class, that didnt work as well.
All these attempts to build the project using maven only lead to a
IllegalState Failed to load ApplicationContext
error for the above test class. Is there any way I could load the environment variables before the Application context gets loaded?