Unit Tests With Springboot

Unit Tests With Springboot

Just had a change to work with my development team to define a process for developing unit tests for a microservices/REST architecture bases application. The central idea was to implement in TDD framework, but I will not talk about TDD in this article, but the aim is jump start someone who is looking to write unit test for springboot application.

When we say automation testing the general feeling you will get is…Ohh its easy, nothing compare to application developer J. Trust me this is one of the most important task of application development life cycle and from my experience I can say that writing good unit test can be considered as an art that take some time and few iterations to master.

But the easy part is that most of the gears supporting unit testing are quite easy to learn.

In this article I will try to provide you with some basic understanding of these gears which will help you to take your first step towards it and later on you can build over it. Please note, these gear/tools will remain unchanged from basic to advance level, but yes you might have to add few more tools as per your specific need. So let’s take our first step:

Step 1: As always, we will start with Maven and dependencies. I used IDE “SpringToolSuite4”, but you are free to use relevant tool of your choice.

No alt text provided for this image
No alt text provided for this image






Please feel free to update to latest version of dependency. Later on when you move to advance level, you might like to add “Lombok”. I leave it to you as mission to know the need of Lombok.

Step 2: I will not go into the details of how to create a springboot project. It is understood that if you are looking into this topic then you must have crossed first hurdle (In actual, creating springboot project is easy, if not done yet take it as assignment.) ?of creating a springboot project. Let see an example of how we can mock database. I am considering that JPA is used.

No alt text provided for this image

Unit test above is written for below service

No alt text provided for this image

Few things to remember as you move toward mastering the art of unit test case writing:

1.??????Avoiding using @Autowired will improve the overall performance of unit test by miles. Because as soon as you say @Autowired you are saying to start the whole application only to @Autowired the object instance into our test. It will take longer once your application starts growing in size and Spring has to load more and more beans into the application context.

The lesion, do not use field injection.

2.??????Always provide constructor and try not to use the @Autowired annotation at all.

No alt text provided for this image

To instantiate RegisterUserCourse object Spring will use this constructor by default when creating application context. Please note, in the earlier version of Spring (prior to sprint 5), @Autowired annotation was needed in the constructor for Sprint to find the constructor. Any why Final, because CourseRepository field content will never change during the lifetime of application. Also the other advance of final is that the compiler will complain if we forgotten to initialize the field, so in a way this will also help us in avoiding accidental programming error.

We have talked about “Lombok” in the earlier part of this article, let me tell you one way in which you can use it to make your unit test more effective. By using @RequiredArgsConstructor annotation of Lombok we can let the construction be automatically generated.

Note: There are lot of suggestion for powerMock to be used with static methods. I will suggest to stick with Mockito. I don’t see any real advantage of powerMock over Mockito because what offered by powerMock can also be done using Mockito with less complexity. These are my thoughts and lot of you might not agree with me.

A problem can be solved in many different ways…depending on how you look at it.

Anubhav Singh Rawat

Automation|DevOps|Sast/Dast|API|Python|Rundeck|Grafana|Unix|Powershell|Excel

3 年

Good and insightful article sandip kumar .

要查看或添加评论,请登录

Sandip kumar的更多文章

社区洞察

其他会员也浏览了