3

I have an Spring Boot application integrated with Hibernate for the database persistence.

I have two different data.sql files:

  • One in src/main/resources for database initialization
  • One in src/test/resources for testing purposes

While testing, both of them are loaded before any test class. However, I only want to load the test/resources one, leaving the main/resources only and solely for app initialization.

How can I do that?

Thanks.

3
  • Can you show your Spring Boot annotations for main and testing? Commented Jul 16, 2018 at 9:08
  • @EstanislaoPérezNartallo the only annotation related to hibernate is spring.jpa.hibernate.ddl-auto=create in both of them Commented Jul 16, 2018 at 9:26
  • @AlbertoCastaño Did you resolve this? Commented Nov 18, 2018 at 15:22

4 Answers 4

0

You should change the spring.jpa.hibernate.ddl-auto=create to update (it only updates the changes from the .sql file), because with create each time that the app runs, "cleans the DB", so thats why the both .sql are processed each time.

I hope this solve your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

No! Maybe I did not explain myself good enough. What I want is to actually create the database from scratch everytime I run my tests, but only from test/resources/data.sql, not from main/resources/data.sql. Right now both of them are being invoked
As @Rahul said, you can't stop loading the main/resources .sql file. Because is needed for the app to run.
0

if test class is starting the spring boot application then i don`t think that you can stop loading the main/resources .sql file.

but if you want to load the .sql file from test/resources just before/after the execution of methods(in test class) then you can use @SqlGroup annotation.

@SqlGroup({
    @Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:beforeTestRun.sql"),
    @Sql(executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:afterTestRun.sql")

          })

Comments

0

You can add the following configuration (at least in spring boot 2) to your test application.yaml or application.properties file.

spring:
  datasource:
    data: data.sql

Once this value is set spring boot will only load the test data.sql when running tests.

Comments

-1

Maintain a separate properties file for test as it will help you in segregating the things between test and development.

application.properties for dev
application-local.properties for local env
application-test.properties for test 

Have profiles according to your need and have properties with respect to profiles.

Another alternative is to override the properties based on your need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.