我对我的 Spring Config 服务器运行了一个测试,以验证应用程序是否正常工作。我做了一些手动测试,在 Spring Confg Boot 应用程序的基本启动中一切正常,但我想要一个单元测试来证明解决方案并能够测试我的开发密钥库等等。
我添加了一个自定义实现以在执行启动时ApplicationContextInitializer
加载数据。application-test.yml
数据未加密时一切正常;但是,当我添加加密属性时,它不会解密它。
我使用的实现是:
public class TestYamlFileApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
try {
Resource resource = applicationContext.getResource(CLASSPATH_URI);
YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
PropertySource<?> testProperties = sourceLoader.load("yamlTestProperties", resource, null);
applicationContext.getEnvironment().getPropertySources().addFirst(testProperties);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
是application-test.yml
:
my:
encrypted:
parameter: '{cipher}AQB8C/1v9J+jPQZG...'
server:
port: 0
spring:
profiles:
active: native
encrypt:
key-store:
location: classpath*:/security/development-test.jks
alias: DevelopmentTest
secret: SomeSecretPassword123
password: SomeStorePassword123
我的测试带有注释@ContextConfiguration (initializers = TestYamlFileApplicationContextInitializer.class)
以启动它。
该测试使用以下命令运行基本检查:
@Autowired
Environment env;
@Test
public void testStuff() {
String theProp = env.getProperty("my.encrypted.parameter");
System.err.println(theProp);
}
输出是: {cipher}AQB8C/1v9J+jPQZG...
没有列出任何例外。
我错过了哪一块拼图?