在SpringBoot Test中加载不同的application.yml

Exi*_*xia 49 spring-mvc spring-boot

我正在使用一个运行我的src/main/resources/config/application.yml的spring启动应用程序.

当我运行我的测试用例时:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
public class MyIntTest{
}
Run Code Online (Sandbox Code Playgroud)

测试代码仍然运行我的application.yml文件来加载属性.我想知道在运行测试用例时是否可以运行另一个*.yml文件.

g00*_*00b 59

一种选择是使用配置文件.创建一个名为application-test.yml的文件,将这些测试所需的所有属性移动到该文件,然后将@ActiveProfiles注释添加到测试类中:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
@ActiveProfiles("test") // Like this
public class MyIntTest{
}
Run Code Online (Sandbox Code Playgroud)

请注意,它还会加载application-test.yml,因此application.yml中的所有属性仍然会被应用.如果您不想这样做,也可以使用配置文件,或者在application-test.yml中覆盖它们.

  • 这是正确的答案.注释TestPropertySource仅适用于.properties或.xml文件.请参阅https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/TestPropertySource.html中的"支持的文件格式部分". (4认同)
  • 这很好用,但如果你这样做,我建议你也有一些**不**使用“测试”配置文件的测试。测试应用程序 yaml 与实际应用程序 yml 合并的事实可能会导致应用程序在单元测试中运行良好,但在独立运行时会出现问题。 (3认同)

The*_*ect 19

您可以在src/test/resources/config/application.yml文件中设置测试属性.Spring Boot测试用例将从测试目录中的application.yml文件中获取属性.

  • 实际上,一开始,用于测试的 yml 文件就在该路径上,即“src/test/resources/config/application.yml”。但是我不知道为什么我运行测试用例时没有加载它 (3认同)
  • 应该将其标记为正确答案,因为YAML文件就是这种情况。我已与Spring Boot Team确认TestPropertySource不支持YAML文件。[Spring Boot JIRA](https://jira.spring.io/browse/SPR-16563) (3认同)
  • 请注意,子目录的名称(`config`)不是任意的,而是预定义的。详情参见【Spring Boot 文档】(https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application -property-files) 和 ConfigFileApplicationListener.load() 方法的源代码。 (2认同)

Ama*_*har 14

您可以使用@TestPropertySource加载不同的属性/ yaml文件

@TestPropertySource(locations="classpath:test.properties")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class MyIntTest{

}
Run Code Online (Sandbox Code Playgroud)

或者,如果您只想覆盖您可以使用的特定属性/ yaml

@TestPropertySource(
        properties = {
                "spring.jpa.hibernate.ddl-auto=validate",
                "liquibase.enabled=false"
        }
)
Run Code Online (Sandbox Code Playgroud)

  • 使用application.yml文件对我不起作用. (7认同)
  • 正如已经解释的那样,这仅适用于 .properties 文件,因为注释不支持 yaml 文件。这确实不是问题的答案。 (6认同)
  • 我尝试使用这个注释,但在测试中它仍然寻找`application.yml`而不是我指定的那个. (3认同)

Ana*_*han 6

Spring-boot框架允许我们提供YAML文件作为.properties 文件的替代,它很方便。属性文件中的键可以在资源文件夹中的application.yml文件中以 YAML 格式提供,spring-boot 会自动取请记住,yaml 格式必须保持空格正确才能正确读取值。

您可以使用@Value("${property}")来从 YAML 文件中注入值。还可以给出Spring.active.profiles来区分不同环境的不同 YAML,方便部署。

出于测试目的,测试 YAML 文件可以命名为application-test.yml并放置在测试目录的资源文件夹中。

如果您application-test.yml在 .yml中指定并提供 spring 测试配置文件,那么您可以使用@ActiveProfiles('test')注释来指示 spring 从您指定的application-test.yml获取配置。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApplicationTest.class)
@ActiveProfiles("test")
public class MyTest {
 ...
}
Run Code Online (Sandbox Code Playgroud)

如果您使用的是 JUnit 5,则不需要其他注释,因为@SpringBootTest已经包含 springrunner 注释。保留一个单独的主 ApplicationTest.class 使我们能够为测试提供单独的配置类,我们可以通过将它们从测试主类中的组件扫描中排除来防止加载默认配置 bean。您还可以提供要在那里加载的配置文件。

@SpringBootApplication(exclude=SecurityAutoConfiguration.class)
public class ApplicationTest {
 
    public static void main(String[] args) {
        SpringApplication.run(ApplicationTest.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是有关使用 YAML 而不是.properties 文件的Spring 文档的链接:https : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html


Lu5*_*u55 6

如果您需要application.yml完全替换生产,则将其测试版本放到相同的路径,但要在测试环境中使用(通常是src/test/resources/

但是,如果您需要覆盖或添加一些属性,则几乎没有选择。

选项1:将测试application.yml放在src/test/resources/config/目录中,如@TheKojuEffect在其答案中建议的那样。

选项2:使用特定application-test.yml配置文件的属性:在您的src/test/resources/文件夹中创建“说” ,然后:

  • @ActiveProfiles在测试类中添加注释:

    @SpringBootTest(classes = Application.class)
    @ActiveProfiles("test")
    public class MyIntTest {
    
    Run Code Online (Sandbox Code Playgroud)
  • 或者spring.profiles.active@SpringBootTest注释中设置属性值:

    @SpringBootTest(
            properties = ["spring.profiles.active=test"],
            classes = Application.class,
    )
    public class MyIntTest {
    
    Run Code Online (Sandbox Code Playgroud)

这不但可以用@SpringBootTest,但用@JsonTest@JdbcTests@DataJpaTest等片测试注释为好。

而且,您可以根据需要设置任意多个配置文件(spring.profiles.active=dev,hsqldb)-有关配置文件的更多信息,请参见。


Pet*_*ete 5

请参阅:使用 YAML 的 Spring @PropertySource

我认为第三个答案有你正在寻找的东西,即有一个单独的 POJO 将你的 yaml 值映射到:

@ConfigurationProperties(path="classpath:/appprops.yml", name="db")
public class DbProperties {
    private String url;
    private String username;
    private String password;
...
}
Run Code Online (Sandbox Code Playgroud)

然后用这个注释你的测试类:

@EnableConfigurationProperties(DbProperties.class)
public class PropertiesUsingService {

    @Autowired private DbProperties dbProperties;

}
Run Code Online (Sandbox Code Playgroud)


DHR*_*SAL 5

一个简单的工作配置使用

@TestPropertySource 和属性

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource(properties = {"spring.config.location=classpath:another.yml"})
public class TestClass {


    @Test

    public void someTest() {
    }
}
Run Code Online (Sandbox Code Playgroud)


Sur*_*yaN 1

我已经测试了@IlyaSerbis 答案,它对我有用。我正在使用 Spring Boot 2.3。

我已经测试了这些场景并且它有效。

场景 1 -首先,我为 jUnit 目的创建application.yml并将其放在src/test/resources/config(与主 application.yml 相同的路径)下,它完全替换了原始 yaml 文件(而不是覆盖)。application.yml这意味着它不会使用目录(主目录)中提到的任何属性src/main/resources/config

注意:Scenario1main和test中application.yml的路径需要完全相同。如果src/main/resources/config/application.yml那么src/test/resources/config/application.yml。如果src/main/resources/application.yml那么src/test/resources/application.yml

场景2 -然后我创建application-test.yml而不是使用application.ymlundersrc/test/resources/config和 using @ActiveProfiles("test"),我可以看到 jUnit 正在从中获取属性值application-test.yml (覆盖公共属性),然后也application.yml从中获取值,这在 中没有提到。application.ymlapplication-test.yml