Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 集成测试的Spring引导默认配置文件_Java_Spring_Spring Boot - Fatal编程技术网

Java 集成测试的Spring引导默认配置文件

Java 集成测试的Spring引导默认配置文件,java,spring,spring-boot,Java,Spring,Spring Boot,SpringBoot利用了这一点,例如,它允许对不同的环境进行单独的配置。 我使用此功能的一种方法是配置集成测试使用的测试数据库。 我想知道是否有必要创建自己的配置文件“test”,并在每个测试文件中显式激活此配置文件? 现在我用以下方法来做: 在src/main/resources内创建application-test.properties 在那里编写特定于测试的配置(现在只需数据库名称) 在每个测试文件中,包括: @ActiveProfiles("test") 有

SpringBoot利用了这一点,例如,它允许对不同的环境进行单独的配置。 我使用此功能的一种方法是配置集成测试使用的测试数据库。 我想知道是否有必要创建自己的配置文件“test”,并在每个测试文件中显式激活此配置文件? 现在我用以下方法来做:

  • 在src/main/resources内创建application-test.properties

  • 在那里编写特定于测试的配置(现在只需数据库名称)

  • 在每个测试文件中,包括:

    @ActiveProfiles("test")
    
  • 有没有更聪明/更简洁的方法?例如,默认测试配置文件


    编辑1:这个问题与Spring Boot 1.4.1有关,据我所知,没有任何东西可以直接解决您的请求,但我可以建议一个有帮助的方案:

    您可以使用自己的测试注释,它由
    @SpringBootTest
    @ActiveProfiles(“test”)
    组成。因此,您仍然需要专用的概要文件,但避免将概要文件定义分散到所有测试中

    此注释将默认为配置文件
    测试
    ,您可以使用元注释覆盖配置文件

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    @SpringBootTest
    @ActiveProfiles
    public @interface MyApplicationTest {
      @AliasFor(annotation = ActiveProfiles.class, attribute = "profiles") String[] activeProfiles() default {"test"};
    }
    

    您可以将application.properties文件放在test/resources文件夹中。你准备好了

    spring.profiles.active=test
    

    这是运行测试时的一种默认测试配置文件。

    另一种方法是定义一个基本(抽象)测试类,您的实际测试类将扩展该测试类:

    @RunWith(SpringRunner.class)
    @SpringBootTest()
    @ActiveProfiles("staging")
    public abstract class BaseIntegrationTest {
    }
    
    混凝土试验:

    public class SampleSearchServiceTest extends BaseIntegrationTest{
    
        @Inject
        private SampleSearchService service;
    
        @Test
        public void shouldInjectService(){
            assertThat(this.service).isNotNull();
        }
    } 
    

    这允许您提取的不仅仅是
    @ActiveProfiles
    注释。您还可以为不同类型的集成测试想象更专业的基类,例如数据访问层与服务层,或功能专业(常见的
    @前
    @后
    方法等)。

    在我的例子中,根据环境,我有不同的application.properties,例如:

    application.properties (base file)
    application-dev.properties
    application-qa.properties
    application-prod.properties
    
    并且application.properties包含一个属性spring.profiles.active以选择适当的文件


    对于我的集成测试,我在
    test/resources
    中创建了一个新的
    application test.properties
    文件,并使用
    @TestPropertySource({“/application test.properties”})创建了一个新的
    application test.properties文件
    annotation这是负责选择应用程序的文件。我需要的属性取决于我对这些测试的需要

    另一种编程方式:

      import static org.springframework.core.env.AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME;
    
      @BeforeClass
      public static void setupTest() {
        System.setProperty(DEFAULT_PROFILES_PROPERTY_NAME, "test");
      }
    

    它工作得很好。

    在application.properties文件中添加
    spring.profiles.active=tests
    ,您可以在spring引导应用程序中添加多个属性文件,如
    application-stage.properties
    application-prod.properties
    ,您可以通过添加
    spring.profiles.active=stage
    spring.profiles.active=prod,在application.properties文件中指定要引用的文件

    您还可以通过提供以下命令在运行spring boot应用程序时传递配置文件:

    java-jar
    -Dspring.profiles.active=local
    build/libs/turtle-rnr-0.0.1-SNAPSHOT.jar

    根据配置文件名获取属性文件,在上述情况下通过“配置文件<代码>本地< /代码>考虑<代码>应用程序本地。属性< /COD>文件< /P> < P>激活“测试”配置文件,在您的构建中写入。

        test.doFirst {
            systemProperty 'spring.profiles.active', 'test'
            activeProfiles = 'test'
        }
    

    这样做的一种方式(事实上,与@Compito的原始答案相比有点小差距):

  • test/resources/application default.properties
    中设置
    spring.profiles.active=test
  • 为测试添加
    test/resources/application test.properties
    ,并仅覆盖所需的属性

  • 如果使用maven,可以将其添加到pom.xml中:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <configuration>
                    <argLine>-Dspring.profiles.active=test</argLine>
                </configuration>
            </plugin>
            ...
    

    而且您不应该需要“-default”属性。

    如果您只是想在通过maven生成时设置/使用默认配置文件,那么请传递参数
    -Dspring.profiles.active=test
    就像

    mvn清理安装-Dspring.profiles.active=dev


    您可以将特定于测试的属性放入
    src/test/resources/config/application.properties

    在测试期间,此文件中定义的属性将覆盖
    src/main/resources/application.properties
    中定义的属性


    有关为什么这样做的更多信息,请看一下。

    2021和SpringBoot2.4版本,我找到的解决方案是有3个属性文件

    • src/main/resources/application.yml
      -包含应用程序的默认道具
    • src/test/resources/application.yml
      -将配置文件设置为“test”,并从“main”导入属性
    • src/test/resources/application test.yml
      -包含特定于测试的配置文件,它将覆盖“main”
    以下是
    src/test/resources/application.yml的内容:

    #对于测试,将默认配置文件设置为“测试”
    spring.profiles.active:“测试”
    #并导入“主”属性
    spring.config.import:file:src/main/resources/application.yml
    
    例如,如果
    src/main/resources/application.yml
    包含

    ip地址:“10.7.0.1”
    用户名:admin
    
    src/test/resources/application test.yml

    ip地址:“999.999.999.999”
    运行集成测试:true
    
    然后(假设没有其他配置文件)

    运行测试时

    profiles=test
    --
    ip-address=999.999.999.999
    username=admin
    run-integration-test=true
    
    以及在正常运行应用程序时

    profiles=none
    --
    ip-address=10.7.0.1
    username=admin
    run-integration-test <undefined>
    
    profiles=none
    --
    ip地址=10.7.0.1
    用户名=管理员
    运行集成测试
    


    注意:如果
    src/main/resources/application.yml
    包含
    spring.profiles.active:“dev”
    ,那么这不会被
    src/test/resources/application test.yml

    覆盖。我通常为所有带有通用代码和注释的集成测试创建基类。不要忘记将其抽象化,以免不稳定。E
    profiles=none
    --
    ip-address=10.7.0.1
    username=admin
    run-integration-test <undefined>
    
    @SpringBootTest
    @Transactional
    @AutoConfigureMockMvc
    @ActiveProfiles("test")
    public abstract class AbstractControllerTest {
    
        @Autowired
        protected MockMvc mockMvc;
    
        protected ResultActions perform(MockHttpServletRequestBuilder builder) throws Exception {
            return mockMvc.perform(builder);
        }
    }
    
    // All annotations are inherited
    class AccountControllerTest extends AbstractControllerTest {
    ....