简体   繁体   中英

Override profile properties in spring boot

In spring boot I have the following profile: application-email.yaml

services:
  email:
    cron: '....'
    recipient: `...`
    other properties...

And in test/resources directory I have application.yaml , I want to include all properties from email profile , but for tests I want to override recipient property.

So I expect my application.yaml to look like this:

spring:
  profiles:
    include: email

services:
  email:
    recipient: `test-email`

As far as I get it, spring at first read application.yaml in test/resources and then overrides it with values from profiles. But I want the opposite.

How can I achieve that?

Addition 1: I think I can create profile test, define my test recipient there. But can I do it without an additional profile?

The best way is indeed to create a application-test.yaml, and activate the profile test.

Why do you dont want to do this? It's the way recommended by spring.

To get all your test properties and the email properties, I'd add add these annotations:

    @ActiveProfiles("test")
    @PropertySource("classpath:application-email.yaml")
    public class MyWonderfulTests { ...}

And given that you only want to override one single property, add the following static block to the top of your test-class:

static {
        System.setProperty("recipient", "test-email");
     }

That should do the trick:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM