Best telugu movie DAHANAM

Dahanam movie is all set to hit the silver screen soon in the mid of Feb 2023, This movie already bagged six international film festival awards . Dahanam is based out of real incidents and now aiming…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How We Started Integration Testing Our Backend Services

Integration testing using an embedded MongoDB, Kotlin, and Spring Boot

Writing good tests is hard but more so is setting up tooling to write tests efficiently. In this article, I will share how we set up Integration tests using flapdoodle’s embedded MongoDB, Kotlin, and Spring Boot. At Inheaden, testing was always a high priority for us. While unit test coverage was always high, we had not invested a lot of time into proper Integration tests. With an increasing load on our servers and additional client projects, we needed to change this to keep operation at high quality.

First up, why even bother with Integration tests? While writing unit tests, especially a lot of good ones, helps to make sure that new code works and does not break the old one, Integration tests are important to test software as a whole. One simply cannot assume that because every part of an application works, the combination of all of them will work as well. So adding good Integration tests to an application will increase your confidence in it to work as expected.

Here, I expect you to be familiar with setting up and running a Spring Boot application using Kotlin. So I will not go into this. What we do need is to install the embedded MongoDB, spring-boot-starter-test and JUnit 5 (using Maven):

This will install the necessary dependency to use an embedded MongoDB when testing your application.

Now we will look at testing a microservice designed to send mail (fittingly called “EMailService”). It, of course, sends mails but also keeps track of every mail sent using a MongoDB. Building an integration test for an application like this poses two challenges:

Using the tools we installed we can solve both of these.

You might note that this is still Java Code. While adding Integration tests into our code base we are also looking into moving to Kotlin. New services and new test code is written in Kotlin only. I will publish our approach to this soon.

Our service exposes (for simplicity) only one API method to the internet: POST /send/email

We model this using a spring boot RestController.

Its one method uses Formdata because we also offer to add attachments using other methods. It then combines all parameters into one request object and passes this to a Service which will handle the actual business logic.

The sendMail method then creates the MimeMessage, adds the email log to the database, and sends the message. Here we see that it also needs the user to be authenticated and to be allowed to send mails.

The @SpringBootTest annotation starts up an application context for your spring boot app using a random port. We configure MockMvc using the next annotation and set a custom *.properties file using the last one. Our IntegrationTestBase autowires a MongoTemplate and the MockMvc. It also calls mongoTemplate.db.drop() after each test.

Where is the embedded MongoDB you might ask? Since we added its dependencies early using test scope it will start automatically when running the tests. It will download the latest version of MongoDB and pick up the database port automatically.

The actual test uses Mockito to mock the mailSender, here a proxy around JavaMailSender. The test runs using a mock user with the correct authorities and tests if a mail would have been send and if the email log has been created.

Since starting our journey for better test coverage using Integration tests, we could set up a lot of tests for different scenarios. Running these tests on our CI system reliably without the need to set up real databases saved us from a lot of hassle. We hope this article helps you in setting up your own Integration tests.

Found this post useful? Kindly hit the 👏 button below to show how much you liked this post!

Inheaden is a young IT and software startup based in Darmstadt, Germany. As an “Idea and Tech Factory”, we have set out to be a driving force of innovation, digitization, and automation with a focus on the areas of services, products, and research. Under the Inheaden brand, we work on individual “high performance” software solutions that bring a change. Modern designs, innovative technology approaches, and IT security for our partners and customers are important components of our work profile.

Add a comment

Related posts:

V4 Bug Bounty Program

The security of CAP and its smart contracts are of utmost importance to us. For that reason, we’ve created an official CAP Bug Bounty Program to incentivize responsible disclosure of vulnerabilities…

New Zealand investors backing blockchain startups

To stay up-to-date on the progress of our technology, follow us on Twitter and our Telegram Announcements channel, plus join our community on Centrality’s Official Telegram, Instagram, Reddit and…

Encrypted PDF Identification and Decryption using Python

Portable Document Format, popularly known as PDF, is the most commonly used data storage formats in modern day and serves numerous benefits. Encryption of the document and its data is one of the…