-1

I suppose that it reads in work working circuit and then it reads in testing circuit.

If application.yml is in testing circuit , it settings replace the settings has been reading in application.yml of working circuit. If You have defined application-test.yml and you point above a class an annotation @SpringBootTest and above @ActiveProfile("test") then the settings from application-test.yml replaces earlier settings.

what do you think about all this ? How to understand this process correctly?

skyho
  • 1,438
  • 2
  • 20
  • 47
  • What is `contour` ? Anyway, maybe read this: https://www.baeldung.com/properties-with-spring – PeterMmm Sep 29 '21 at 18:18
  • a contour is a logical area of the project where the application or tests are located. These are 2 different places in the context of the project. For link that you have pointed, there is little info: ..."4.3. Test-Specific Properties File We might also have a requirement to use different property values when our application is under test. Spring Boot handles this for us by looking in our src/test/resources directory during a test run. Again, default properties will still be injectable as normal but will be overridden by these if there is a collision." - I'm not sure I got it right. – skyho Sep 29 '21 at 18:50

3 Answers3

1
  • Terminology:

Work Circuit : /src/main/resources

Test Circuit : /src/test/resources

  • Work Circut

application.yaml

main:
  car:
    name: "Audi"
    id: "10201"

application-local.yaml

main:
  car:
    name: "Mercedes"
    id: "10200"
  • main class
@SpringBootApplication
public class SpringProfilesApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringProfilesApplication.class, args);
    }

    @Bean
    CommandLineRunner commandLineRunner(ProfileMain profileMain) {
        return args -> {
            profileMain.printMainName();

        };
    }
}
  • class
@Component
public class ProfileMain {

    @Value("${main.car.name}")
    private String name;

    @Value("${main.car.id}")
    private String id;

    public void printMainName() {
        System.out.println("\n name in Main application.yaml : " + this.name + "\n");
        System.out.println("\n name in Main application.yaml : " + this.id + "\n");
    }
}

– When you run the main class, without specifying any profiles, application.yaml will be used. – You can specify the profile you need in the development environment

enter image description here

Now the settings of those properties that were in the main application.yaml, will be overridden in the specified profile - local. If the selected profile does not contain the settings that are in the main application.yaml, then the missing properties will be taken from the main application.yaml.

  • Test circuit
@SpringBootTest(classes = SpringProfilesApplication.class)
class ProfileMainTest {

    @Value("${main.car.name}")
    private String name;

    @Value("${main.car.id}")
    private String id;

    @Test
    void printMainName() {
        System.out.println("\n In test circuit : " + name);
        System.out.println("\n In test circuit : " + id);
    }

When we do not explicitly specify a profile, the settings will be taken from the main application.yaml.

Now we use the *.yaml file from the test loop.

application-test.yaml

main:
  car:
    name: "Nissan"
    id: "10203"
@SpringBootTest(classes = SpringProfilesApplication.class)
@ActiveProfiles("test")
class ProfileMainTest {
...
}

Here the properties (if any) will be redefined, that is, taken from the application-test.yaml file. But if there is no property, then it will be taken from the main file application.yml.

  • Resume

In the Working circuit

  1. If the profile is not specified, the settings will be taken from the main application.yaml.
  2. If a profile is specified, then the settings from this profile will override the settings from the main application.yaml file. If some settings are missing, then the settings will be taken from the main application.yaml file(if any).

In the test circuit

  1. If the profile is not specified, the settings will be taken from the main application.yaml.
  2. If a profile is specified, then the settings from this profile will override the settings from the main application.yaml file. If some settings are missing, then the settings will be taken from the main application.yaml file (if any).
skyho
  • 1,438
  • 2
  • 20
  • 47
0

According to Spring Boot reference documentation, the configuration data files are considered in the following order:

  1. Application properties packaged inside your jar (application.properties and YAML variants).
  2. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  3. Application properties outside of your packaged jar (application.properties and YAML variants).
  4. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
João Dias
  • 16,277
  • 6
  • 33
  • 45
-1

you have to put this in your application-test.yml

spring:
  profiles: test