0

In my Spring Boot program I'm getting a failure due to a bad property value on load. In particular, it uses the DB2 hibernate dialect but it's not defined in the property file I thought I was using.

Assuming no annotations, where does Spring look for the properties file? Yes I know it normally resides in src/main/resources/application.properties

What if I have a property in my test cases; does it ignore the one in main and use the one in test? Or does it start with the main version and let the test one override the main where it applies?

Does the application profile affect the property file used? Some people use the same application.properties file name in both main and test.

If I do have a TestSource annotation with a class path location, does it still augment it with something somewhere else?

Finally, how can I get Spring to tell me everywhere it looked for properties and not just one of them?

Woodsman
  • 901
  • 21
  • 61
  • 1
    filename is not `application.property` , it is `application.properties` – sanjeevRm Jun 17 '21 at 01:20
  • purpose of using profile is to use separate application properties based on environment, so yes profile does affect properties file used – sanjeevRm Jun 17 '21 at 01:22
  • if you have profiles defined, then it will look for properties file which is referred in active profile, else it will look for `application.properties` – sanjeevRm Jun 17 '21 at 01:26
  • @sanjeevRm I corrected the misspelling. After asking this, I realized it's possible to specify a profile in the properties file. – Woodsman Jun 17 '21 at 01:34
  • 1
    The order in which spring boot considers property sources is well documented [here](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config). You can specify defaults and override them in numerous ways. It's a maven convention that you put an application.properties under src/test/resources that is used for tests instead of the one in src/main/resources. It's up to the build tool or IDE as to which properties file to use when running tests. – Hopey One Jun 17 '21 at 02:01

1 Answers1

1

@Woodsman

It's possible to use many settings in each profile/environment in spring application on src/main/resources/ folder

application-dev.properties
application-local.properties
application-onlytests.properties
application-prd.properties
application.properties

the name after hyphen will be the name profile got by spring

Ex: application-{myenviroment}.properties

When you start spring application will be used application.properties by default.

You can tell spring to use an specific properties passing the environment in one of ways below:

  • Putting spring.profiles.active=prd inside application.properties file
  • Passing by parameters when start spring app --spring.profiles.active=local
  • Running your jar on command line java -jar myjar.jar --spring.profiles.active=dev
  • Setting an environment var in your machine/docker/container SET SPRING_ACTIVES_PROFILE=local
  • There are other ways using annotations on beans, passing jvm arguments and others

If you need run your tests in a specific configuration ( vars, database, settings ), It's possible to pass which .properties will be used

@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@SpringBootTest
@TestPropertySource(locations = "classpath:application-onlytests.properties")
public class RunTest_from_onlytests_properties {

    @Autowired
    private MockMvc mockMvc;
    
    @Test // org.junit.jupiter.api.Test
    public void test() throws Exception{
      // ...
    }
}
Dilermando Lima
  • 1,004
  • 8
  • 21