a3dsfcv
a3dsfcv

Reputation: 1274

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?

Upvotes: 1

Views: 419

Answers (2)

Johnny Alpha
Johnny Alpha

Reputation: 970

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 :)

Upvotes: -1

ggr
ggr

Reputation: 292

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.

Upvotes: 3

Related Questions