Search code examples
springspring-bootunit-testingdaospring-jdbc

Spring Boot Rest application: Testing JDBC DAO layer


How can I test my DAO layer in Spring Boot Application if my application only selects information from database and doesn't write anything?

Even more, my application selects data from view.

The common approach is to write some testing data by method with annotation @BeforeEach and to delete them by method with annotation @AfterEach.

But because my application performs query to view, I can't insert any data in database.

Is there any opportunity to test my DAO layer?


Solution

  • You have a few options:

    1. Use an embedded H2 database then seed it with a data.sql, which you can dump from your test database.

    2. Use DBUnit and define your data in an xml file.

    For you I think data.sql is the way to go. Just add a data.sql to your test/resources file and it will be picked up by JPA.