<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ana Marija Lugonja on Medium]]></title>
        <description><![CDATA[Stories by Ana Marija Lugonja on Medium]]></description>
        <link>https://medium.com/@amlugonja?source=rss-9c9c5343c28e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*BzTVXxSzSI0o_RlE2xS4vQ.jpeg</url>
            <title>Stories by Ana Marija Lugonja on Medium</title>
            <link>https://medium.com/@amlugonja?source=rss-9c9c5343c28e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 02:02:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amlugonja/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[YOUR PATH TO BLACK BELT BEGINS HERE: JAVA, MONGODB AND KARATE DSL]]></title>
            <link>https://medium.com/clarityai-engineering/your-path-to-black-belt-begins-here-java-mongodb-and-karate-dsl-a455b5a5eef6?source=rss-9c9c5343c28e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a455b5a5eef6</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[karate-dsl]]></category>
            <dc:creator><![CDATA[Ana Marija Lugonja]]></dc:creator>
            <pubDate>Tue, 19 May 2020 08:54:41 GMT</pubDate>
            <atom:updated>2021-05-26T12:17:41.661Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Karate logo" src="https://cdn-images-1.medium.com/max/935/1*JkKkC9qxZvSi9FJ1QkST1w.png" /></figure><h3>Your path to black belt begins here: Java, MongoDB and Karate DSL</h3><p>When I first heard of <a href="https://github.com/intuit/karate/tree/master/karate-demo">Karate</a> DSL I was thinking “what do martial arts have to do with coding?” After a few silly jokes that I’ll spare you, in <a href="https://clarity.ai/">Clarity AI</a> we realised it’s a great tool for implementing <a href="https://en.wikipedia.org/wiki/Functional_testing">functional tests</a>.</p><p>Why? You may ask. Because it has big variety of functionalities such as gherkin-style tests, powerful DSL to access and validate json responses and is easily extensible and customizable to our needs.</p><p>After learning more about Karate and following our culture of embracing new technology in Clarity AI, we decided to give Karate a try. How to manage MongoDB basic functions in Karate? How to mix<em> </em>Java implementation and Karate functionalities? These and other questions popped up in my mind in the beginning of the Karate development in our system that was using MongoDB. After resolving those questions I fell like sharing my experience for those who can benefit from it. Hope it helps!</p><h3>BASIC KARATE CONCEPTS</h3><p>Karate has three basic concepts: <em>Feature</em>, <em>Background</em> and <em>Scenario.</em></p><ul><li><em>Feature</em> is an extension of the files that contain tests. Feature can contain one or more <em>scenarios</em>.</li><li><em>Scenario</em> is a test case in the Karate feature.</li><li><em>Background </em>is used for preparing tests execution like defining variables, reading external files, executing other features. It is executed for each scenario.</li></ul><p>There are also other functionalities like <em>afterScenario</em> and <em>afterFeature</em> that we will mention in the article. <em>AfterScenario</em> contains functions that are executed after a scenario is executed. <em>AfterFeature</em> contains functions that are executed after a feature is done executing all scenarios. Both functionalities are useful if you want to delete data or reset values after scenario or feature is done running.</p><h3>JAVA IMPLEMENTATION</h3><h4><strong>KARATE CONFIGURATION</strong></h4><p>In order to connect to a Mongo database, it is necessary to add these dependencies to the project:</p><pre>implementation(<strong>&quot;com.fasterxml.jackson.core:jackson-databind:2.8.8&quot;)<br></strong>implementation(<strong>&quot;org.slf4j:slf4j-api:1.7.25&quot;)<br></strong>implementation(<strong>&quot;org.mongodb:mongo-java-driver:3.6.3&quot;</strong>)</pre><p>Note that we also added dependencies for the json and logs so it’s easier to track errors and to manage the json files used in the application. They are optional but we recommend them.</p><h4><strong>MANAGING DATABASE CONNECTION</strong></h4><p>First of all, we will need a connection to the database. As the connection is not the objective of this article we’re not going to dedicate time to showing different examples of how to implement it. In our case, we opted for a simple connection with only the host and the port as necessary data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc97904beb90593112eea8ffef140c22/href">https://medium.com/media/bc97904beb90593112eea8ffef140c22/href</a></iframe><h4><strong>INSERTING DATA INTO THE DATABASE</strong></h4><p>Once the connection is established, we have to insert data into the database. We recommend implementing a generic method to insert the data so it could be applied in different databases and collections with different objects. In this case, it is necessary to convert our list of objects to MongoDB documents to be able to insert them into the database.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3079f50433ae0fb14abfac05d9498e3f/href">https://medium.com/media/3079f50433ae0fb14abfac05d9498e3f/href</a></iframe><h4><strong>READING DATA FROM THE DATABASE</strong></h4><p>In the next snippet, you can see an example of reading data from the database. We are going to obtain _id from MongoDB and return the value of it into the Karate test. In order to get the necessary id, we need to <em>find</em> the document by searching with <em>unique field (for example _id) </em>as shown in the <em>Example 3</em>. Once the data is available, it is going to be used as a parameter in the endpoint that you can see in <em>Example 11</em>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97f57b4112f02a5436894ed3baa8d023/href">https://medium.com/media/97f57b4112f02a5436894ed3baa8d023/href</a></iframe><h4><strong>DELETING DATA FROM THE DATABASE</strong></h4><p>After the test is finished, we remove the data from the database. If the collection only contains the data that was inserted for the test, the whole collection should be removed. Otherwise, be sure to provide unique IDs to delete only test data.</p><p>In the next example, you can see a method that deletes various documents. It checks if the field name (passed in the method’s argument) of the document contains ids and if it does, the documents that contain them will be deleted.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3b6226bffb923523bf6f2125bf655e1/href">https://medium.com/media/a3b6226bffb923523bf6f2125bf655e1/href</a></iframe><h4><strong>DISCONNECTING FROM THE DATABASE</strong></h4><p>Since we connected to the database with the method, we will need another one to disconnect from it. That method already exists in MongoDB and we are going to encapsulate it in a method as shown in the next example.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27d6262e1bcf91947dc171a2fbc634a5/href">https://medium.com/media/27d6262e1bcf91947dc171a2fbc634a5/href</a></iframe><p>Here you can see how the full example of the class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88c3bdf526d2c0e691115dcf6ddabb3f/href">https://medium.com/media/88c3bdf526d2c0e691115dcf6ddabb3f/href</a></iframe><h3><strong>IMPLEMENTING THE TEST IN KARATE FEATURE</strong></h3><h4>CONNECTING AND DISCONNECTING TO THE DATABASE IN KARATE FEATURE</h4><p>It is inefficient to connect and disconnect to a database for each individual scenario. Instead, we establish a connection with the database in the background section, then disconnect from the database after all scenarios have run in the <em>afterFeature</em> functionality that Karate offers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2503f844b202b4d91ac6ec20b7612694/href">https://medium.com/media/2503f844b202b4d91ac6ec20b7612694/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2649cd76f1659c39653333524d0215be/href">https://medium.com/media/2649cd76f1659c39653333524d0215be/href</a></iframe><h4>INSERTING DATA INTO THE DATABASE IN A KARATE FEATURE</h4><p>In each scenario, all necessary data is inserted into the database before calling the endpoint. In this case, the data that we are going to insert is in json format and we have stored it in the application’s resources.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b8783e224082a480cb7c454ff575702/href">https://medium.com/media/0b8783e224082a480cb7c454ff575702/href</a></iframe><p>In order to get the data from the json shown previously, you should invoke the method to read en external file. The method’s name is <em>read</em> and it is already implemented in Karate for this purpose. It is only necessary to pass the path of the file you want to upload to the test into the method. After the data from the json is recovered, it can be easily inserted into the database by calling the java method implemented in the <em>Example 2.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00f9173f50db183350e6050c80f2e74a/href">https://medium.com/media/00f9173f50db183350e6050c80f2e74a/href</a></iframe><h4>GETTING DATA FROM THE DATABASE IN A KARATE FEATURE</h4><p>You can see how to get data from MongoDB by passing it the database name, collection, field name and field value you want search for in the<em> Example 10 </em>at the line 4.</p><h4>DELETING DATA FROM THE DATABASE IN A KARATE FEATURE</h4><p>As mentioned before, after the endpoint call, we delete the data. Note that we are deleting data in the <em>afterScenario</em> functionality that Karate provides. Data will be deleted after the scenario is finished because we do not want the data from this scenario to affect other scenarios that might be run afterwards.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d251537c9c26efeae5077234a66a179/href">https://medium.com/media/1d251537c9c26efeae5077234a66a179/href</a></iframe><p>The full example of Karate feature is below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53df583f1e660f13e2ca9ed3198038c1/href">https://medium.com/media/53df583f1e660f13e2ca9ed3198038c1/href</a></iframe><h3><strong>CONCLUSION</strong></h3><p>This article explains how to create a test using Karate, Java and MongoDB. Karate has a mechanism that allows you to implement almost any functionality that you want simply by adding the right dependencies to the project and creating Java classes that Karate features can use.</p><p>In Clarity AI, Karate helped us make our testing better and we have grown to use it for every new feature we are making. Hopefully this article helps you to implement your tests and to see the range of functionalities that Karate can provide as it did for us.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a455b5a5eef6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/clarityai-engineering/your-path-to-black-belt-begins-here-java-mongodb-and-karate-dsl-a455b5a5eef6">YOUR PATH TO BLACK BELT BEGINS HERE: JAVA, MONGODB AND KARATE DSL</a> was originally published in <a href="https://medium.com/clarityai-engineering">Clarity AI Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>