0

I'm trying to test a method with @SpringBootTest and @ActiveProfile("test") in my class. When I run the new tests, everything works fine and the properties are loaded from the test YML file correctly. But when I run all tests, those same tests load the properties from the production YML file.

I've tried using @DirtiesContext, with BEFORE_CLASS and BEFORE_EACH_METHOD flags, on the new test class but it doesn't seem to help.

I have something similar to the following:


@ActiveProfiles("test")
@SpringBootTest
public class NewTestClass{

     ...

    @Test
    public void testThatLoadsPropertiesAndShouldLoadApplicationTestYmlFile() {
    ...
    }

}

What am I missing here? is there a simple way of solving this problem?

UPDATE

I disabled the other Integration Tests on the project using @Disable from Junit 5 and checked if the problem persisted. surprisingly, it did. So I'm suspecting it is not an ApplicationContext caching problem.

  • Please create `application.yml` file on test resources `/src/test/resources/` – reflexdemon Nov 14 '19 at 16:02
  • @reflexdemon I have this file configured. The problem is that when I run only the tests of the class annotated with `@activeProfiles("test")`, spring loads the test file as you suggested, but when I run all tests, which contain other Integration tests, spring seems to load the file on `/src/main/resources`. – Leonardo Meinerz Ramos Nov 14 '19 at 16:28

2 Answers2

0

Having the problem description provided I can only suggest the that there might be another test suite configured so that the context is setup using the production config (yml). Please consult this answer (the question raised is similar in a way...): https://stackoverflow.com/a/43332643/8085853

Pavel B
  • 1
  • 1
  • If I understood that answer, this problem shouldn't be happening, because the active profile is one of the keys for the application context caching strategy. So, because I have the "test" profile active for this class and in the other ones I have the default profile, Spring probably **IS NOT** using the same context for these different tests. – Leonardo Meinerz Ramos Nov 14 '19 at 16:39
0

Ok, I believe I found the cause of the problem. I'm running all tests using IntelliJ, but "all tests" is being managed by the JUnit plugin and the single tests are being managed by the Gradle plugin. Once I used gradle to run "all tests", everything worked.