【问题标题】:SpringBoot 1.5+ junit test does not load configuration with custom nameSpring Boot 1.5+ junit 测试不加载自定义名称的配置
【发布时间】:2018-06-01 08:46:10
【问题描述】:

我正在设置 Spring Boot 应用程序,我希望使用自定义文件名而不是 application.yml 进行配置。原因是运行这些应用程序的服务器端 tomcat。

我定义了应用程序类:

@SpringBootApplication(scanBasePackages={"com.example"})
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        System.setProperty("spring.config.name", "myapp");
        SpringApplication.run(Application.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.properties("spring.config.name: myapp").sources(Application.class);
    }
    ...

和名为 myapp.yml 的配置

在 JUnit 测试中我有以下注释:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
@ContextConfiguration(initializers = {ConfigFileApplicationContextInitializer.class})
@AutoConfigureMockMvc
public class UserTest {
...

以及 junit 的 pom 依赖项:

    <dependency>
        <groupId>org.jmockit</groupId>
        <artifactId>jmockit</artifactId>
        <version>1.31</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.7.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.7.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>1.5.3.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-mockmvc</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
<!-- if I remove this dependency then normal application works with custom name but if I have it in dependencies then mvn spring-boot:run does not find configuration either -->
    <dependency>
        <groupId>org.yaml</groupId>
        <artifactId>snakeyaml</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>

我尝试了许多不同的方法,但无法按我的意愿工作。我想要我自己的配置文件名,然后使用 @ActiveProfiles 注释解决不同的配置。谢谢!

【问题讨论】:

  • 你的main 方法在开始测试时没有执行,所以显然在那里设置东西是行不通的。使用 @BeforeClass 注释的方法来设置所需的属性。
  • 我没有检查 main 是否真的发布了!如果我检查一下,我会自己找出其余的。感谢您提供有用的信息!

标签: java spring spring-boot junit


【解决方案1】:

正如@M.Deinum 提到的,您应该在测试类的@BeforeClass 方法中初始化属性:

@BeforeClass
public static void initProperties() {
    System.setProperty("spring.config.name", "myapp"); 
}

好的做法是在测试后整理属性,这样其他测试就不会受到这些属性的影响

@AfterClass
public static void clearProperties() {
    System.clearProperty("spring.config.name"); 
}

但是使用测试属性有更好的选择:

您也应该在测试中使用 application.yaml。您的动态配置(例如数据库凭据)不应放在此配置文件中。它们应该通过 env 属性传递给应用程序(因此12 factor app principles)。

在测试期间,有多种方法可以定义这种动态配置。例如:

  1. 上面提到的机制@BeforeClass + System.setPRoperty
  2. 或者您可以通过@TestProperties 注释或SpringBootTest.properties 属性使用额外的测试配置文件

【讨论】:

  • 这对我有用。我把它放在抽象类中,所以每次我想使用“测试”配置文件时都会调用它。感谢您的快速回复!干杯
【解决方案2】:

假设您有名为 application-test.yml 而不是 application.yml 的自定义配置。

您只需将以下内容添加到您的测试类中,

@SpringBootTest(value={"spring.profiles.active=test"})

这样您就可以避免设置配置属性并在@BeforeTest@AfterTest 中专门重新设置它们。

希望对你有帮助!

【讨论】:

  • 这是真的,但如果我想使用自定义名称而不是默认的“应用程序”,只有这不起作用。接受的答案还解释了为什么这不起作用。谢谢
猜你喜欢
  • 2017-02-12
  • 1970-01-01
  • 2019-04-26
  • 2021-12-23
  • 2018-11-14
  • 2019-02-20
  • 2019-01-30
  • 1970-01-01
  • 2021-09-05
相关资源
最近更新 更多