【问题标题】:junit integration test spring property not resolvedjunit集成测试spring属性未解决
【发布时间】:2020-05-22 15:37:49
【问题描述】:

我有一个看起来像这样的简单集成测试

@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 并在一天中调用它,但我想直接在测试类上执行它,因为参数可以从一个测试类更改为另一个,我真的不想要很多不同的@ 987654327@测试配置文件。

还有其他人遇到过这个问题吗?

编辑

Deadpool 的回答解决了这个问题,因为它设置了实际的占位符,所以在一堆不同的属性键可能具有从同一个占位符派生的值的情况下,他的回答是要走的路。设置每个 some.property.key=PLACEHOLDER 也可以,直到你忘记一个,这就是我所做的。

【问题讨论】:

    标签: java spring-boot unit-testing junit4


    【解决方案1】:

    一旦你在application.yml 中有属性的占位符

    some:
     property:
      key: ${PLACEHOLDER}
    

    您应该使用该占位符来替换值

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = MyIntTestConfig.class, properties = {PLACEHOLDER=value})
     public class MyIntTest {
    
        @Autowired
        public MyComponent myComponent;
    
          // ...
    
     }
    

    【讨论】:

      猜你喜欢
      • 2014-12-25
      • 2016-01-03
      • 2014-04-04
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多