我应该如何将环境属性添加到我的 JUnit5 Spring MVC 集成测试中?
How should I add environment properties to my JUnit5 Spring MVC integration test?
我正在 JUnit 5 中使用 MockMvcBuilders.webAppContextSetup()
编写集成测试。
我正在扩展 Sam Brannen 的 SpringExtension
和 MockitoExtension
。 (真的,我正在使用组合的 SpringJUnitJupiterWebConfig)
我在 运行 测试(已编辑)时得到此输出:
java.lang.IllegalStateException: Failed to load ApplicationContext
BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.example.myapp.config.SomeConfig];
Could not resolve placeholder 'someEnvVar' in string value "classpath:/com/example/myapp/config/${someEnvVar}/custom.properties"
(这是我的应用程序所依赖的公司内部库。)
很明显我需要在容器启动之前设置环境 属性。但我正在努力寻找如何使用 JUnit5 连接到它。
我想将 PropertySources 添加到环境中,我想,但是要获得环境,我必须获得应用程序上下文,这样做时,它会实例化,在我可以对其进行任何操作之前出错。
在 BeforeAllCallback
期间,我尝试创建自己的扩展并获取环境句柄。
我感觉我的做法全错了,我遗漏了一些基本的东西。
我只需要在我的测试中使用 @TestPropertySource
注释 class。
我正在 JUnit 5 中使用 MockMvcBuilders.webAppContextSetup()
编写集成测试。
我正在扩展 Sam Brannen 的 SpringExtension
和 MockitoExtension
。 (真的,我正在使用组合的 SpringJUnitJupiterWebConfig)
我在 运行 测试(已编辑)时得到此输出:
java.lang.IllegalStateException: Failed to load ApplicationContext
BeanDefinitionStoreException: Failed to process import candidates for configuration class [com.example.myapp.config.SomeConfig];
Could not resolve placeholder 'someEnvVar' in string value "classpath:/com/example/myapp/config/${someEnvVar}/custom.properties"
(这是我的应用程序所依赖的公司内部库。)
很明显我需要在容器启动之前设置环境 属性。但我正在努力寻找如何使用 JUnit5 连接到它。 我想将 PropertySources 添加到环境中,我想,但是要获得环境,我必须获得应用程序上下文,这样做时,它会实例化,在我可以对其进行任何操作之前出错。
在 BeforeAllCallback
期间,我尝试创建自己的扩展并获取环境句柄。
我感觉我的做法全错了,我遗漏了一些基本的东西。
我只需要在我的测试中使用 @TestPropertySource
注释 class。