Testing persistence layer

73 views
Skip to first unread message

Eric Deandrea

unread,
Mar 15, 2021, 10:26:03 AM3/15/21
to Quarkus Development mailing list
I'm interested in what people are doing for testing their persistence layer with Quarkus - specifically Hibernate/Panache

1) There is an existing database with an existing schema that has a bunch of tables
2) I'm building an application and I'm going to use JPA, so I write some POJO classes and put all the JPA annotations on them
3) How do I test/validate/assert that those JPA annotations I put on my POJO classes actually bind to the schema/tables in the way I expect, without having to deploy my application into an "environment"?

In Spring, I would accomplish this by using H2. I would have H2 auto-create my schema in my tests based on my code. I would then run 2 test scenarios

1) Wire in my Spring Data JPA JpaRepository and use it to persist an entity, then use Spring's JdbcTemplate to run SELECTs queries based on the schema I expected to pull the data

2) Use Spring's JdbcTemplate to issue INSERTs into the appropriate tables, then use the JpaRepository to read the data inserted

How would I do something like this in Quarkus? Essentially this is a test that the JPA annotations on my entity classes are correct. I trust that Hibernate itself is working the way it should :) I haven't yet found a good way to accomplish the same thing in Quarkus. Do people not test this kind of capability? Or do I just not know of a better way?

I have a simple example [1] of a single entity with a single table, but think about multiple entities where there are foreign keys, join tables, column constraints, etc.

[1] https://github.com/edeandrea/spring-boot-demo/blob/main/src/test/java/com/example/demo/repository/PersonRepositoryTests.java


Eric Deandrea

Sr. Principal Technical Marketing Manager

Red Hat North America

edea...@redhat.com    M: 978.237.4727    T: 978.392.1070    


Stephane Epardaud

unread,
Mar 15, 2021, 11:11:11 AM3/15/21
to edea...@redhat.com, Quarkus Development mailing list
Hi,

Myself I would have used H2 in my test profile (you can have per-profile configuration and only enable H2 when running tests) and test things on H2 for fast startup with a real DB.

Using test-containers works wonders too, if you don't mind the extra load.

--
You received this message because you are subscribed to the Google Groups "Quarkus Development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to quarkus-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/quarkus-dev/CAOTfCRVXw4FURG%3D_dBYvevpN3a6jQ5UQ1ic%2BtMmVauyfrCo%3DRA%40mail.gmail.com.


--
Stéphane Épardaud

Loïc MATHIEU

unread,
Mar 15, 2021, 12:14:16 PM3/15/21
to Stephane Epardaud, edea...@redhat.com, Quarkus Development mailing list
Hi,

+1 H2 or testcontainers, I used testcontainers successfully with a Posgres database as I also use flyway to manage the schema of my DB so I prefere to check my flyway script hence the usage of testcontainers instead of H2 but both are good tools (H2 will start quicker).

We don't have a simple way to interact with the database as with Spring JdcTemplate, this has been raised a few times already maybe you can open an issue to gather feedback on it.
Maybe you can use Apache DBUtil for this, https://commons.apache.org/proper/commons-dbutils/index.html, you can easily inject a Datasource inside your test and use it.

Regards,

Loïc

Reply all
Reply to author
Forward
0 new messages