junit 集成测试 spring 属性 未解决
junit integration test spring property not resolved
我有一个简单的集成测试,如下所示
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyIntTestConfig.class, properties = {some.property.key=value})
public class MyIntTest {
@Autowired
public MyComponent myComponent;
// ...
}
@Configuration
public class MyIntTestConfig {
@MockBean
private MyComponent myComponent;
@Bean
public myComponent() {
// ...
return myComponent;
}
}
在application.yml
中有
some:
property:
key: ${PLACEHOLDER}
当我 运行 这个测试 mvn clean test
我得到错误
Could not resolve placeholder 'PLACEHOLDER' in value "${PLACEHOLDER}"
许多其他答案建议添加一个 application-test.yml
并收工,但我想直接在测试 class 上进行,因为参数可以从一个测试 class 更改为另一个,我真的不想要很多不同的 .yml
测试配置文件。
还有其他人遇到过这个问题吗?
编辑
Deadpool 的回答解决了这个问题,因为它设置了实际的占位符,所以在一堆不同的 属性 键可能具有从同一个占位符派生的值的情况下,他的回答是可行的。设置每个 some.property.key=PLACEHOLDER
也有效,直到你忘记一个,这就是我所做的。
一旦您在 application.yml
中拥有属性的占位符
some:
property:
key: ${PLACEHOLDER}
您应该使用该占位符来替换值
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyIntTestConfig.class, properties = {PLACEHOLDER=value})
public class MyIntTest {
@Autowired
public MyComponent myComponent;
// ...
}
我有一个简单的集成测试,如下所示
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyIntTestConfig.class, properties = {some.property.key=value})
public class MyIntTest {
@Autowired
public MyComponent myComponent;
// ...
}
@Configuration
public class MyIntTestConfig {
@MockBean
private MyComponent myComponent;
@Bean
public myComponent() {
// ...
return myComponent;
}
}
在application.yml
中有
some:
property:
key: ${PLACEHOLDER}
当我 运行 这个测试 mvn clean test
我得到错误
Could not resolve placeholder 'PLACEHOLDER' in value "${PLACEHOLDER}"
许多其他答案建议添加一个 application-test.yml
并收工,但我想直接在测试 class 上进行,因为参数可以从一个测试 class 更改为另一个,我真的不想要很多不同的 .yml
测试配置文件。
还有其他人遇到过这个问题吗?
编辑
Deadpool 的回答解决了这个问题,因为它设置了实际的占位符,所以在一堆不同的 属性 键可能具有从同一个占位符派生的值的情况下,他的回答是可行的。设置每个 some.property.key=PLACEHOLDER
也有效,直到你忘记一个,这就是我所做的。
一旦您在 application.yml
some:
property:
key: ${PLACEHOLDER}
您应该使用该占位符来替换值
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyIntTestConfig.class, properties = {PLACEHOLDER=value})
public class MyIntTest {
@Autowired
public MyComponent myComponent;
// ...
}