使用Spring应用程序属性而不启动服务器

问题描述

我们正在使用Spring Boot 2.3.4

我们有一些单元测试,只需要加载application.properties(以及所有的Profile Management)。 是否可以在不启动完整的Spring Boot服务器的情况下做到这一点?

编辑:

我尝试了@SpringBoottest批注,但是它总是启动整个服务器,因为我引用了@SpringBootApplication类(或者它是自动引用的)。 实际上,您不需要所有其他注释,例如@ConfigurationProperties

您需要的只是 @SpringBootApplication(classes = AClass.class),其中AClass是任何没有主要方法的类,例如您的测试类。

解决方法

使用@SpringBootTest注释测试类以加载上下文。