The Myth of Greener Grass

My client Quinton was never quite satisfied. Even when he was, clearly, satisfied, he always wondered if he could be more satisfied. He moved twice in one year because he found an apartment he liked…

Smartphone

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




Test your REST API that you wrote in Kotlin

Beginner level tutorial on how to add Unit Tests to your REST API in Kotlin and Spring Boot.

Level: Beginner
Language: Kotlin
Time needed: 30 minutes
Coverage: Basics, Spring boot, Unit Tests

As part of my collection around writing a REST API in Kotlin, this is part #2, you can find the first story right here on Medium.

First off all; all the things that are needed to run unit tests in your Kotlin application has already been prepared for you by Spring Boot. In the project you’ll find a file in src/test/kotlin with a .kt extension that holds a entry point to your test, in case of our example project, that file is called: TutorialRestApiApplicationTests.kt every file you place in this folder that ends with *Tests.kt will be used to run unit tests.

Again Spring Boot has everything covered for you, the @SpringBootTest makes sure that it is aware of a test file, and will put everything in place to have the test run. You’ll also find another annotation: @Test this is to tell the test runner; jUnit (Spring boot uses jUnit for unit tests); to take the next function and execute it as a Unit test. To learn more about unit tests with Spring Boot.

First let’s us run the test, before we start adding our own test function.

Again we use the terminal, and execute the following:

$ ./gradlew test
you should see something similar as the screenshot below.

Nothing much has happened, as the provided test with the project we created with Spring Boot Initializr only holds an empty test case, but it will tell us that the whole testing cycle has run perfectly fine, and we are ready to start adding our own tests.

Tip: once the tests have competed, Gradle will prepare a nice little report on the unit tests. You’ll need the browser to see it, but it is located at: {projectDir}/build/reports/tests/test once you open the index.html file, you will see a report as the example in the screenshot below:

Now we will add a new unit test to test our status endpoint that we added in our previous tutorial. Below snippet will show how the file will look once the test has been added:

As you can see there is a lot more to see now in the file, that will run our test of our status endpoint, I’ll explain:

Now that I’ve explained what we are doing, we are ready to run our tests again. We use the same command as previously:

$ ./gradlew test

If everything went ok, you will see the same result as before, if something would have gone wrong, it will show you something like the screenshot below:

Something didn’t work out in our Unit Test

I’ve did change the test a tiny bit, just to show you how it will look if there would be a test failure, the generated html report I mentioned before will hold more information:

Error reported in our test report

As you can see I removed the ‘!’ on the expected message string, once I put the ‘!’ back, and run the test again, the reports looks great and will confirm that all our tests have run perfectly fine.

Successful test report

Congratulations, you have created your first Unit Test, you know a little bit more about how Spring Boot handles these tests. There is a lot more possible under the hood with this, but I have tried to keep it basic, but this should give you a lot of info on how to proceed and start testing all the new endpoints you have created since our first tutorial 😉

And be sure to follow me, to get notified when we are going to dive deeper into our REST API to add more features in following stories.

Add a comment

Related posts:

About LuckyHash

The masses have a high expectation towards “Ethereum 2.0”. But a common misperception is “Ethereum 2.0 will reduce gas fees.” Two things you need to know: First, Ethereum upgrade is a long process…

Showing my work

Now that The Heart to Start is safely out in the world (now available everywhere), it’s time to revisit Getting Art Done. I set up yet another Medium publication. I’ll be writing over there about my…