【发布时间】:2021-06-03 11:04:43
【问题描述】:
我正在使用 Spring Boot 2.4.0 开发多模块 maven 项目。我已经为一个模块编写了集成测试。测试类与此类似。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringApplicationClassWithMainMethod.class)
public class XYZServiceIT {
@Test
public void test1() {...}
@Test
public void test2() {...}
}
要运行 SpringApplicationClassWithMainMethod.class,即,为了加载应用程序上下文,我需要在 eclipse 中设置的几个环境变量。因此,为了在加载 SpringApplicationClassWithMainMethod.class 时运行上述集成测试,我需要在加载 Application 上下文之前使用这些环境变量。
试用一: 我尝试使用 @TestPropertySource(properties = {"key1=val1", "key2=val2"}) 注释,但没有用。
试用 2: 我也尝试过静态块来设置不起作用的环境变量。
试用 3: 我还尝试将 @ContextConfiguration 与 ApplicationContextInitializer 类一起使用,但效果不佳。
所有这些使用 maven 构建项目的尝试只会导致
IllegalState 无法加载 ApplicationContext
上述测试类的错误。有什么方法可以在加载应用程序上下文之前加载环境变量?
【问题讨论】:
标签: java spring spring-boot maven integration-testing