我有一个Spring-Boot应用程序,其中在应用程序中设置了默认属性。属性文件在类路径(src/main/resources/application.properties)。

我想用测试中声明的属性覆盖JUnit测试中的一些默认设置。属性文件(src/test/resources/test. Properties)

我通常为我的Junit测试有一个专用的配置类,例如。

package foo.bar.test;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {

}

我最初认为在TestConfig类中使用@PropertySource("classpath:test.properties")可以达到目的,但是这些属性不会覆盖应用程序。属性设置(参见Spring-Boot参考文档- 23。外部化配置)。

然后我尝试使用-Dspring.config.location=classpath:test。属性。这是成功的——但是我不想为每次测试执行都设置这个系统属性。因此我把它放在代码中

@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {

  static {
    System.setProperty("spring.config.location", "classpath:test.properties");
  }

}

不幸的是,这次也没有成功。

必须有一个简单的解决方案如何覆盖应用程序。使用test在JUnit测试中设置属性。我一定是忽略了这些特性


当前回答

我认为你也可以用这个:

@TestPropertySource(properties = "spring.config.additional-location=classpath:application-test.yml")

当使用spring.config配置自定义配置位置时。附加位置,它们用于默认位置之外。

该文件将具有优先级

详情请参考此处。

其他回答

TLDR:

我所做的就是创建标准的src/main/resources/application。属性和src/test/resources/application-default。属性,我覆盖一些设置为我所有的测试。

power-developers:

为了更容易地改变/使用不同的spring配置文件,我现在有一个应用程序默认。它声明了我想要使用的概要文件。 这个文件是不提交的,所以每个开发人员可以选择他/她正在工作的配置文件和需求(例如功能)的激活方式。

spring:
  profiles:
    include:
      - local
      - devlocal
      - wip
#      - kafka@docker

---
spring.profiles: wip
# ... overriding properties 

整个故事

我遇到了同样的问题,到目前为止也没有使用配置文件。现在必须这样做,并记得声明概要文件似乎很麻烦——这很容易忘记。

诀窍在于,利用特定于概要文件的应用程序-<profile>。属性将覆盖常规配置文件中的设置。见https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html # boot-features-external-config-profile-specific-properties。

您可以使用@TestPropertySource来覆盖application.properties中的值。从它的javadoc:

测试属性源可用于选择性地覆盖系统和应用程序属性源中定义的属性

例如:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ExampleApplication.class)
@TestPropertySource(locations="classpath:test.properties")
public class ExampleApplicationTests {

}

如果您正在使用Spring 5.2.5和Spring Boot 2.2.6,并且希望只覆盖一些属性而不是整个文件。您可以使用新的注释:@DynamicPropertySource

@SpringBootTest
@Testcontainers
class ExampleIntegrationTests {

    @Container
    static Neo4jContainer<?> neo4j = new Neo4jContainer<>();

    @DynamicPropertySource
    static void neo4jProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.data.neo4j.uri", neo4j::getBoltUrl);
    }
}
I just configured min as the following :

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console


# changing the name of my data base for testing
spring.datasource.url= jdbc:h2:mem:mockedDB
spring.datasource.username=sa
spring.datasource.password=sa



# in testing i don`t need to know the port

#Feature that determines what happens when no accessors are found for a type
#(and there are no annotations to indicate it is meant to be serialized).
spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false`enter code here`

使用

使用.yaml file-ending声明的属性 Spring-Boot 2.7 类路径中有多个.yaml文件

我注意到@TestPropertySource(位置)的优先级没有像使用.properties文件一样被应用。

我遇到的问题是,Spring一直在加载所有的.yaml属性(特别是那些来自产品的属性),并使用为prod指定的值覆盖用于测试的属性。

我们提出了通过指定我的应用程序测试来覆盖配置抓取机制的变通方法。Yaml作为唯一的属性使用如下:

@TestPropertySource(properties = "spring.config.location=classpath:/application-test.yaml")