Java 用于集成测试的 Spring-boot 默认配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39690094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:21:18  来源:igfitidea点击:

Spring-boot default profile for integration tests

javaspringspring-boot

提问by Piotr Zakrzewski

Spring-boot utilizes Spring profiles (http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html) which allow for instance to have separate config for different environments. One way I use this feature is to configure test database to be used by integration tests. I wonder however is it necessary to create my own profile 'test' and explicitly activate this profile in each test file? Right now I do it in the following way:

Spring-boot 使用 Spring 配置文件(http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html),例如允许针对不同环境具有单独的配置。我使用此功能的一种方法是配置测试数据库以供集成测试使用。但是我想知道是否有必要创建我自己的配置文件“测试”并在每个测试文件中明确激活此配置文件?现在我通过以下方式做到这一点:

  1. Create application-test.properties inside src/main/resources
  2. Write test specific config there (just the database name for now)
  3. In every test file include:

    @ActiveProfiles("test")
    
  1. 在 src/main/resources 中创建 application-test.properties
  2. 在那里编写测试特定的配置(现在只是数据库名称)
  3. 在每个测试文件中包括:

    @ActiveProfiles("test")
    

Is there a smarter / more concise way? For instance a default test profile?

有没有更聪明/更简洁的方法?例如默认测试配置文件?

Edit 1: This question pertains to Spring-Boot 1.4.1

编辑 1:这个问题与 Spring-Boot 1.4.1 有关

采纳答案by Mathias Dpunkt

As far as I know there is nothing directly addressing your request - but I can suggest a proposal that could help:

据我所知,没有什么可以直接解决您的要求 - 但我可以提出一个可以帮助的提案:

You could use your own test annotation that is a meta annotationcomprising @SpringBootTestand @ActiveProfiles("test"). So you still need the dedicated profile but avoid scattering the profile definition across all your test.

您可以使用自己的测试注释,该注释是包含@SpringBootTest和的元注释@ActiveProfiles("test")。因此,您仍然需要专用配置文件,但要避免在所有测试中分散配置文件定义。

This annotation will default to the profile testand you can override the profile using the meta annotation.

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

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

回答by Compito

You could put an application.properties file in your test/resources folder. There you set

您可以将 application.properties 文件放在 test/resources 文件夹中。你在那里设置

spring.profiles.active=test

This is kind of a default test profile while running tests.

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

回答by Pierre Henry

Another way to do this is to define a base (abstract) test class that your actual test classes will extend :

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

@RunWith(SpringRunner.class)
@SpringBootTest()
@ActiveProfiles("staging")
public abstract class BaseIntegrationTest {
}

Concrete test :

具体测试:

public class SampleSearchServiceTest extends BaseIntegrationTest{

    @Inject
    private SampleSearchService service;

    @Test
    public void shouldInjectService(){
        assertThat(this.service).isNotNull();
    }
} 

This allows you to extract more than just the @ActiveProfilesannotation. You could also imagine more specialised base classes for different kinds of integration tests, e.g. data access layer vs service layer, or for functional specialties (common @Beforeor @Aftermethods etc).

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

回答by Eduardo

In my case I have different application.properties depending on the environment, something like:

就我而言,根据环境,我有不同的 application.properties,例如:

application.properties (base file)
application-dev.properties
application-qa.properties
application-prod.properties

and application.properties contains a property spring.profiles.active to pick the proper file.

和 application.properties 包含一个属性 spring.profiles.active 来选择正确的文件。

For my integration tests, I created a new application-test.propertiesfile inside test/resourcesand with the @TestPropertySource({ "/application-test.properties" })annotation this is the file who is in charge of picking the application.properties I want depending on my needs for those tests

对于我的集成测试,我在application-test.properties里面创建了一个新文件,test/resources并带有@TestPropertySource({ "/application-test.properties" })注释,这是负责选择 application.properties 的文件,具体取决于我对这些测试的需求

回答by Valtoni Boaventura

Another programatically way to do that:

另一种以编程方式执行此操作的方法:

  import static org.springframework.core.env.AbstractEnvironment.DEFAULT_PROFILES_PROPERTY_NAME;

  @BeforeClass
  public static void setupTest() {
    System.setProperty(DEFAULT_PROFILES_PROPERTY_NAME, "test");
  }

It works great.

它工作得很好。

回答by Bishal Jaiswal

Add spring.profiles.active=testsin your application.properties file, you can add multiple properties file in your spring boot application like application-stage.properties, application-prod.properties, etc. And you can specify in your application.properties file while file to refer by adding spring.profiles.active=stageor spring.profiles.active=prod

添加spring.profiles.active=tests在您的application.properties文件,你可以在你喜欢春天启动的应用程序添加多个属性文件 application-stage.propertiesapplication-prod.properties等等。你可以在你的application.properties指定文件,而文件中加入参考spring.profiles.active=stagespring.profiles.active=prod

you can also pass the profile at the time running the spring boot application by providing the command:

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

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

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

According to the profile name the properties file is picked up, in the above case passing profile localconsider the application-local.propertiesfile

根据配置文件名称拾取属性文件,在上述情况下传递配置文件local考虑application-local.properties文件

回答by Demel

To activate "test" profile write in your build.gradle:

要激活“测试”配置文件,请在 build.gradle 中写入:

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

回答by JohnW

A delarative way to do that (In fact, a minor tweek to @Compito's original answer):

一种声明性的方式来做到这一点(实际上,@Compito 的原始答案只是一个小小的 tweek):

  1. Set spring.profiles.active=testin test/resources/application-default.properties.
  2. Add test/resources/application-test.propertiesfor tests and override only the properties you need.
  1. 设置spring.profiles.active=testtest/resources/application-default.properties.
  2. 添加test/resources/application-test.properties测试并仅覆盖您需要的属性。

回答by Bojan Vukasovic

If you use maven, you can add this in pom.xml:

如果你使用 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>
        ...

Then, maven should run your integration tests (*IT.java) using this arugument, and also IntelliJ will start with this profile activated - so you can then specify all properties inside

然后,maven 应该使用这个参数运行你的集成测试(*IT.java),并且 IntelliJ 也将从这个配置文件激活开始 - 这样你就可以在里面指定所有属性

application-test.yml

and you should not need "-default" properties.

并且您不需要“-default”属性。

回答by Rajan Mishra

If you simply want to set/use default profile at the time of making build through maven then, pass the argument -Dspring.profiles.active=testJust like

如果您只是想在通过 maven 进行构建时设置/使用默认配置文件,则传递参数 -Dspring.profiles.active=test就像

mvn clean install -Dspring.profiles.active=dev

mvn clean install -Dspring.profiles.active=dev