无法解析值“ /api/${version.v1}”中的占位符“ version.v1”

时间:2019-11-27 17:18:49

标签: java spring-boot spring-mvc mockito

我正在尝试使用Mockito编写Springboot端点单元测试。系统在测试执行上下文期间未能初始化占位符。您能帮我了解我要去哪里了吗?

@SpringJUnitWebConfig
@ContextConfiguration(classes=MockLocationsController.class)
@TestPropertySource(properties = {
        "version.v1=v1",    
    })
public class MockLocationsControllerTest {

}
  

原因:java.lang.IllegalArgumentException:无法解决   占位符'version.v1'的值为“ / api / $ {version.v1}”

1 个答案:

答案 0 :(得分:0)

通过提供自定义@ContextConfiguration,您创建了一个有限的Spring Test上下文,该上下文很可能没有PropertyPlaceholderConfigurer bean。没有那个bean属性,占位符将无法解析。

通常,使用@WebMvcTest声明带有模拟的Web层的Spring Boot测试,这使您使用PropertyPlaceholderConfigurer的工作设置最少。

@SpringJUnitWebConfig
@WebMvcTest(MockLocationsController.class)
@TestPropertySource(properties = {
        "version.v1=v1",    
    })
public class MockLocationsControllerTest {

}