3

Working on unit tests in Spring boot application and came across an issue: I put

application.properties 
data.sql

files under src/main/resources

and

application-test.properties
data-test.sql

files under src/test/resources

But realized when @SpringBootTest starting it anyway uses data.sql from src/main/resources Browsing internet ( and this site particularly) I found the solution:

  1. use @ActiveProfiles("test") with @SpringBootTest
  2. spring.datasource.platform=test in application-test.properties

But even after that I realized data.sql is still working. Actually I see the right way: use profiles for everything: test, dev, etc. And put profile=dev in run configuration and rename data.sql to data-dev.sql

So may be may be it worth to go further and manually define everything: 3.@TestPropertySourcelocations="classpath:test.properties"

OR

@TestPropertySource(properties = {
                "spring.jpa.hibernate.ddl-auto=validate",
                "liquibase.enabled=false"
        }
)

OR

@Sql({"/data-test.sql"})

So my question is: is putting files under src/test/resources gives nothing in prior of src/main/resources? And anyway need to care about profiles ( which seems a silver bullet in this context). Why spring doesn't understand that it should use resources from src/test/resources while testing?

(from my point now it seems completely misleading such secularization to src/test/resources and src/main/resources which seems looks promising but gives nothing... what I'd like to see: put file at main and expect it in main app, put the file with the same name under test and expect it uses while uni testing only)

J.J. Beam
  • 2,612
  • 2
  • 26
  • 55
  • 2
    You have put a profile specific file in `src/test/resources`. When running tests the classpath is the combination of `test` and `main` where `test` is taking precedence. So if there is no `data.sql` in `src/test/resources` it will look in the remainder of the classpath for it. The `data.sql` and `application.properties` are always loaded. – M. Deinum Dec 09 '19 at 06:58
  • Does the ----- 1)`src/test/resources` is taking precedence & 2) `data.sql` and `application.properties` are always loaded ------ together mean: regardless what is under `src/test/resources` the `src/main/resources/application.properties` & `src/main/resources/data.sql` will be always picked? – J.J. Beam Dec 09 '19 at 08:25
  • springboot auto look for file by file name - if the file name is the same in test and main resources folder, it chooses firstly from `main`, secondly from `test` - the file name must be specific for the test – Marek Bernád Feb 03 '22 at 13:19

0 Answers0