{"id":1005,"date":"2020-02-17T15:35:02","date_gmt":"2020-02-17T14:35:02","guid":{"rendered":"https:\/\/www.loicmathieu.fr\/wordpress\/?p=1005"},"modified":"2020-02-21T13:49:54","modified_gmt":"2020-02-21T12:49:54","slug":"quarkus-et-testcontainers","status":"publish","type":"post","link":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/quarkus-et-testcontainers\/","title":{"rendered":"Quarkus and Testcontainers"},"content":{"rendered":"<p>If you did not know Quarkus, here is an introductory article: <a href=\"https:\/\/blog.zenika.com\/2019\/04\/23\/zoom-sur-quarkus\/\" target=\"_blank\" rel=\"noopener noreferrer\">Zoom sur Quarkus<\/a> (french).<\/p>\n<p>Quarkus offers Unit Test (TU) support with JUnit 5 via the <a>@QuarkusTest<\/code>code&gt;@QuarkusTest&lt;\/code<\/a> annotation, documentation for Quarkus TU support can be found <a href=\"https:\/\/quarkus.io\/guides\/getting-started-testing\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<p>Here is an example of a TU from the <a href=\"https:\/\/github.com\/quarkusio\/quarkus-quickstarts\/blob\/master\/hibernate-orm-quickstart\" target=\"_blank\" rel=\"noopener noreferrer\">Hibernate ORM Quickstart<\/a> :<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@QuarkusTest\npublic class FruitsEndpointTest {\n\n    @Test\n    public void testListAllFruits() {\n        given()\n              .when().get(\"\/fruits\")\n              .then()\n              .statusCode(200)\n              .body(\n                    containsString(\"Cherry\"),\n                    containsString(\"Apple\"),\n                    containsString(\"Banana\")\n                    );\n    }\n}\n<\/pre>\n<p>Conventionally, you can run tests using an embedded database, Quarkus supports the H2 BDD for that. But if we want our tests to run in an environment as close as possible to production, the use of the same database is better.<\/p>\n<p>We would therefore like to be able to use a Postgres database to run our tests.<\/p>\n<p><a href=\"https:\/\/www.testcontainers.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Testcontainers<\/a> is a library that allows you to easily start a Docker container within your TUs. It provides a JUnit 5 extension and an extension to easily launch databases. Its use would therefore be ideal for launching a Postgres database for our TUs.<\/p>\n<p>The classical integration of Testcontainers is done via the <a>@Testcontainers<\/code>code&gt;@Testcontainers&lt;\/code<\/a> annotation which allows to start the JUnit 5 extension for Testcontainers and allows to start containers via <a>@Container<\/code>code&gt;@Container&lt;\/code<\/a>, see the <a href=\"https:\/\/www.testcontainers.org\/test_framework_integration\/junit_5\/\" target=\"_blank\" rel=\"noopener noreferrer\">documentation of the Junit 5 extension of Testcontainers<\/a>.<\/p>\n<p>The issue is that the two extensions, the Quarkus one and the Testcontainers one, do not work very well together because Quarkus will start before Testcontainers and the container will not be started before the Quarkus application starts. The database will therefore not be started when your application starts, so your application will fails to start.<\/p>\n<p>With JUnit 5, we should be able to order the extensions between them via <a>@ExtendWith<\/code>code&gt;@ExtendWith&lt;\/code<\/a> but it is not possible to use this annotation with Testcontainers (more info <a>here<\/a>).<\/p>\n<p>The solution ? Using a modified JDBC URL for Testcontainers (documented <a href=\"https:\/\/www.testcontainers.org\/modules\/databases\/\" target=\"_blank\" rel=\"noopener noreferrer\"> here <\/a> ).<\/p>\n<p>For our example we must first add the Testcontainers dependencies to our <code> pom.xml <\/code>.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n    \n      org.testcontainers\n      testcontainers\n      ${testcontainers.version}\n      test\n    \n    \n      org.testcontainers\n      junit-jupiter\n      ${testcontainers.version}\n      test\n    \n    \n      org.testcontainers\n      postgresql\n      ${testcontainers.version}\n      test\n    \n<\/pre>\n<p>Then add the annotation <code> @Testcontainers <\/code> to our <code> FruitsEndpointTest <\/code>.<\/p>\n<p>Finally configure our datasource via our <code> application.properties <\/code> using a modified JDBC URL for Testcontainers:<\/p>\n<pre>\nquarkus.datasource.url = jdbc:tc:postgresql:11.3:\/\/hostname\/mydatabase\nquarkus.datasource.driver = org.testcontainers.jdbc.ContainerDatabaseDriver\nquarkus.hibernate-orm.dialect = org.hibernate.dialect.PostgreSQLDialect\n<\/pre>\n<p><strong>Attention, you must then specify the Hibernate dialect because the automatic dialect selection does not work when the JDBC URL is modified.<\/strong><\/p>\n<p>Once all this is configured, when Quarkus initializes its datasource, Testcontainers will automatically start a Postgres container and you will see the following lines in your logs:<\/p>\n<pre>\n2020-02-17 15:03:20,078 INFO  [org.tes.doc.DockerClientProviderStrategy] (Agroal_5751340441) Loaded org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy from ~\/.testcontainers.properties, will try it first\n2020-02-17 15:03:20,283 INFO  [org.tes.doc.EnvironmentAndSystemPropertyClientProviderStrategy] (Agroal_5751340441) Found docker client settings from environment\n2020-02-17 15:03:20,283 INFO  [org.tes.doc.DockerClientProviderStrategy] (Agroal_5751340441) Found Docker environment with Environment variables, system properties and defaults. Resolved dockerHost=unix:\/\/\/var\/run\/docker.sock\n2020-02-17 15:03:20,374 INFO  [org.tes.DockerClientFactory] (Agroal_5751340441) Docker host IP address is localhost\n2020-02-17 15:03:20,390 INFO  [org.tes.DockerClientFactory] (Agroal_5751340441) Connected to docker: \n[...]\n2020-02-17 15:03:20,958 INFO  [org.tes.DockerClientFactory] (Agroal_5751340441) Ryuk started - will monitor and terminate Testcontainers containers on JVM exit\n2020-02-17 15:03:20,970 INFO  [\ud83d\udc33 .3]] (Agroal_5751340441) Creating container for image: postgres:11.3\n2020-02-17 15:03:21,009 INFO  [\ud83d\udc33 .3]] (Agroal_5751340441) Starting container with ID: c40f75e48f238f2aafe14899adae0afd3a3948396c19247aff64811166d4b057\n2020-02-17 15:03:21,390 INFO  [\ud83d\udc33 .3]] (Agroal_5751340441) Container postgres:11.3 is starting: c40f75e48f238f2aafe14899adae0afd3a3948396c19247aff64811166d4b057\n2020-02-17 15:03:23,038 INFO  [\ud83d\udc33 .3]] (Agroal_5751340441) Container postgres:11.3 started in PT2.067585S\n<\/pre>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>If you did not know Quarkus, here is an introductory article: Zoom sur Quarkus (french). Quarkus offers Unit Test (TU) support with JUnit 5 via the @QuarkusTestcode&gt;@QuarkusTest&lt;\/code annotation, documentation for Quarkus TU support can be found here. Here is an example of a TU from the Hibernate ORM Quickstart : @QuarkusTest public class FruitsEndpointTest { @Test public void testListAllFruits() { given() .when().get(&quot;\/fruits&quot;) .then() .statusCode(200) .body( containsString(&quot;Cherry&quot;), containsString(&quot;Apple&quot;), containsString(&quot;Banana&quot;) ); } } Conventionally, you can run tests using an embedded database,&#8230;<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/quarkus-et-testcontainers\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[9],"tags":[11,167,46,177],"class_list":["post-1005","post","type-post","status-publish","format-standard","hentry","category-informatique","tag-java","tag-quarkus","tag-test","tag-testcontainers"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1560,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/quarkus-tip-tester-une-fonction-google-cloud\/","url_meta":{"origin":1005,"position":0},"title":"Quarkus Tip: Testing a Google Cloud function","author":"admin","date":"Thursday December 29th, 2022","format":false,"excerpt":"I recently contributed a PR to Quarkus that contains a testing framework for Google Cloud functions. Quarkus supports creating Google Cloud functions three different ways: Using the Google Cloud API. Using a Quarkus HTTP extension: RESTEasy, Reactive routes, Servlet, Spring Web. Using Funqy, the cloud provider agnostic Quarkus function API.\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1440,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/google-cloud-functions-2nd-gen\/","url_meta":{"origin":1005,"position":1},"title":"Google Cloud Functions 2nd gen","author":"admin","date":"Tuesday March 29th, 2022","format":false,"excerpt":"Sorry, this entry is only available in Fran\u00e7ais.","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/loicmathieu.fr\/wordpress\/wp-content\/uploads\/cloud-function-gen2-instances.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/loicmathieu.fr\/wordpress\/wp-content\/uploads\/cloud-function-gen2-instances.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/loicmathieu.fr\/wordpress\/wp-content\/uploads\/cloud-function-gen2-instances.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2089,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/deploy-a-quarkus-application-in-cloud-run\/","url_meta":{"origin":1005,"position":2},"title":"Deploy a Quarkus application in Cloud Run","author":"admin","date":"Tuesday December 30th, 2025","format":false,"excerpt":"Quarkus is a microservice development framework designed for the cloud and containers. It is designed to have reduced memory usage and the shortest possible startup time. It is mainly based on standards (Jakarta EE, Eclipse MicroProfile, etc.) and allows the use of mature and widely used Java libraries via its\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1353,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/quarkus-tip-comment-ne-pas-creer-une-extension-quarkus\/","url_meta":{"origin":1005,"position":3},"title":"Quarkus Tip: How NOT to create a Quarkus extension","author":"admin","date":"Tuesday November 16th, 2021","format":false,"excerpt":"When you develop an application composed of several components, it is frequent to want to share some code in an external library, for example via an external JAR integrated as a dependency of your components. Quarkus is an extension framework, each extension it offers allows to integrate a technology (BDD\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1345,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/quarkus-et-les-google-cloud-functions\/","url_meta":{"origin":1005,"position":4},"title":"Quarkus and  the Google Cloud Functions","author":"admin","date":"Tuesday November  2nd, 2021","format":false,"excerpt":"Quarkus is a microservice framework designed for the cloud and the containers. It is designed to have a reduced memory usage and the shortest possible startup time. It is mainly based on standards (Jakarta EE, Eclipse MicroProfile, ...) and allows the use of mature and widespread Java libraries via its\u2026","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":975,"url":"https:\/\/www.loicmathieu.fr\/wordpress\/informatique\/devfest-nantes-2019-developper-une-api-cloud-ready-avec-quarkus\/","url_meta":{"origin":1005,"position":5},"title":"(Fran\u00e7ais) Devfest Nantes 2019 &#8211; D\u00e9velopper une API Cloud Ready avec Quarkus","author":"admin","date":"Thursday October 31st, 2019","format":false,"excerpt":"Sorry, this entry is only available in Fran\u00e7ais.","rel":"","context":"In &quot;informatique&quot;","block_context":{"text":"informatique","link":"https:\/\/www.loicmathieu.fr\/wordpress\/category\/informatique\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts\/1005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/comments?post=1005"}],"version-history":[{"count":0,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/posts\/1005\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/media?parent=1005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/categories?post=1005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.loicmathieu.fr\/wordpress\/wp-json\/wp\/v2\/tags?post=1005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}