Spring integration tests with profile

前端 未结 5 1823
温柔的废话
温柔的废话 2020-12-07 21:40

In our Spring web applications, we use the Spring bean profiles to differentiate three scenarios: development, integration, and production. We use them to connect to differe

5条回答
  •  情歌与酒
    2020-12-07 22:03

    As other people have already pointed out, you can opt to use Maven to set the spring.profiles.active system property, making sure not to use @ActiveProfiles, but that's not convenient for tests run within the IDE.

    For a programmatic means to set the active profiles, you have a few options.

    1. Spring 3.1: write a custom ContextLoader that prepares the context by setting active profiles in the context's Environment.
    2. Spring 3.2: a custom ContextLoader remains an option, but a better choice is to implement an ApplicationContextInitializer and configure it via the initializers attribute of @ContextConfiguration. Your custom initializer can configure the Environment by programmatically setting the active profiles.
    3. Spring 4.0: the aforementioned options still exist; however, as of Spring Framework 4.0 there is a new dedicated ActiveProfilesResolver API exactly for this purpose: to programmatically determine the set of active profiles to use in a test. An ActiveProfilesResolver can be registered via the resolver attribute of @ActiveProfiles.

    Regards,

    Sam (author of the Spring TestContext Framework)

提交回复
热议问题