JUnit Testcases Best Practices Guide

Posted By : Kaushlendra Kumar Pal | 29-Jul-2018

“Unit testing is basically used to test individual units of source code to determine if they are fit or not for use in programming.”

 

For example: In procedural programming more commonly a unit is an individual function or a procedure but also it could be a whole module and In Object Oriented Programming a unit could be an individual method but commonly it's an entire interface such as a class. So in unit testing, a unit is the smallest testable part of an application.

 

Unit testing is not about finding bugs only:

Firstly, if you are starting unit testing then you must know the main motive behind unit testing and you should keep in mind that unit testing is not only an effective way to find bugs in source code or to detect regression. The unit test basically examines each unit of your source code separately. In real when your application running all the units of your application should work fine together and this is more complex than the tested parts independently. It's not necessary that if individual components working fine independently then all the units of test cases are compatible with each other or configured correctly.

It's very effective to run the entire application together same as production while trying to finding bugs same as you normally do in manual testing. But in future to detect breakages in your application you should automate this which is called integration testing in which you have many different techniques and technologies available to use than unit testing.

 

Test cases improvements for writing great test cases:

 

1.Test only one code unit at a time: Running test cases should be enough when you do some changes in pre-written source code or do refactoring it then this will help to test that functionality has not broken anywhere.

 

2. Don’t make unnecessary assertions: If you don’t want to end up multiple test case failures for a single reason then you should just focus on what you are testing so never try to assert everything. Otherwise writing test cases will not help in achieving anything.

 

3. Make each test independent of all the others: If you are writing test cases then you have to keep in mind that never make the chain of test cases. Then if your test case will fail then to find the root cause of test failure will be very difficult for you and you have to debug the source code. It will also make a big problem for you because it will create a dependency between test cases unnecessarily and you have to make change multiple test cases if you make a change in one test case.

 

4. Mock out all external services and state: While writing test cases it is important to mock out external services and state because if your test case is failing due to bugs in some external system then you will not like to debug that test case due to external failure.

 

5. Name your unit tests clearly and consistently: In test cases, you should keep in mind and follow the points that you must name your test cases on what they are actually doing and what they are testing. E.g. It Should be like this: 1) TestCreateUser_NullId_ShouldThrowException 2) TestCreateUser_NegativeId_ShouldThrowException 3) TestCreateUser_DuplicateId_ShouldThrowException 4) TestCreateUser_ValidId_ShouldPass

 

6. Write tests for methods that have the fewest dependencies first, and work your way up: While writing test cases its most important principle to follow to save your test case failure unnecessarily you should always write tests for methods which are less dependent to other first and then work your way up. Suppose if you are testing Users Module then you should first write for creating user then get user then update user and at last you have to create delete user test which will also clean your DB from test data and this flow of test cases has minimum dependency on other test cases and also test cases will have minimum possibility of failure.

 

7. Aim for each unit test method to perform exactly one assertion: It’s not necessary to follow or a thumb rule but to avoid to wasting time on debugging that what is going wrong in test case failures you should always try to test only one thing in one test case. Never try to test multiple things in a single test case.

 

8. Ensure that test code is separated from production code: If you don’t want to waste your resources then ensure that you are not deploying your test code with your actual source code in your build script.

 

9. Do not print anything out in unit tests: You should not include the print statement in your test cases because if you are following all the guidelines of test cases then you will never need to print anything.

 

10. Do not use static members in a test class or re-initialize again for each test case: Now you already know that test cases should not depend on each other so its recommended to never use static data members in your test cases.

 

11. Integrate Testcases with build script: For reliability of your application, it’s better to integrate your test cases with your build script. So it will get executed automatically for your production environment.

 

About Author

Author Image
Kaushlendra Kumar Pal

Kaushlendra is an experienced Java Developer with comprehensive mastery of all Java packages. Able to Write well designed, testable, efficient code as per the requirements and handle different types of issues as well as functions.

Request for Proposal

Name is required

Comment is required

Sending message..