Spring boot series — Unit testing basics

Dimuthu Wickramanayake
Jun 6 · 2 min read

Hi guys. So up to now we have developed a basic spring boot app which gets data from Yahoo finance API and show the stock price. So in this tutorial i’m just going to touch the unit testing area a little bit because when you work for a big tech company these practices comes handy. So here in this tutorial i will be using JUnit for unit testing. So if you look at the gradle.build file i have already shown you guys, It includes JUnit dependencies. So lets dive directly in to a test scenario in our code. Tutorials up to now in this series,

So the other day we created a service to get Stock data from Yahoo fiance API. So i will be adding unit tests for that file. Guys i’m not here to go deeply in to basics and make this tutorial boring. So i will show you the easy way to create a test file using intellij. Go to the StockService.java file and then right click inside the class. In the popup you will see a tab called “Generate” click on it. There you will find a tab called Test and click on it. So you will get a dialog box like this.

So here you can see its asking us for which methods we need unit tests. As we have only one method we are going to write the unit test to that.

This is what the code look like just after creating the test file. So in spring boot @Test annotation is invoking the test in the build time. As the data we get for stock values are changing every minute, i will be testing for the symbol we get from Yahoo finance API is equal to the symbol i’m giving as the input. So the code for that is as follows.

So now go to the terminal/CMD windows users type.

For ubuntu users type,

Just change the value of “GOOG” in assert and again run gradle test you will see test will fail. So this is basic introduction to unit testing. Happy coding guys. Next time we will improve our stock controller to send data to a front end application in JSON form.

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Dimuthu Wickramanayake

Written by

I'm a software engineer at Sysco LABS. Graduated from faculty of enginering University of Peradeniya.

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.