有没有办法可以在 Spring Boot 测试中从 src/test/resources/application.properties 加载属性?

问题描述

    @ExtendWith(SpringExtension.class)
@WebMvcTest(MyClass.class)
@TestPropertySource(locations="classpath:application-test.yml")

//是这样的吗?

解决方法

这应该仍然能够在您的应用程序的测试文件中找到属性:

@Autowired
private Environment env;

// Load data
String data = env.getProperty("some.property")