I have pretty much the same problem like this user. Hibernate can\'t drop the tables of my in-memory test database after each SpringBootTest (e.g. when running mvn test). Th
I finally found a work around for my problem.
As I said, the errors where caused by trying to drop tables on which other tables still depend on. I thought that this could be related to missing CascadeType
specifcations, but I couldn't fix it.
Then I found this answer and it works for me. Additional to my data.sql
file (where all my data is inserted to the auto-created schema) I now have a drop-tables.sql
where I can specify the correct order of DROP
statements. This file is executed before the automatic schema creation and therefore solves my problem.
application.properties:
spring.jpa.properties.javax.persistence.schema-generation.database.action=drop-and-create
spring.jpa.properties.javax.persistence.schema-generation.drop-source=script-then-metadata
spring.jpa.properties.javax.persistence.schema-generation.drop-script-source=drop-tables.sql
You must annotate your test classes with DirtiesContext:
@DirtiesContext(methodMode = MethodMode.BEFORE_CLASS)
This will rebuild the test context and therefore also create-drop your schema.
Read more about this: https://www.baeldung.com/spring-dirtiescontext