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:
- use
@ActiveProfiles("test")
with @SpringBootTest 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)