Why Unit Testing?

Posted on Posted in Unit Testing

It is natural for the new developers to think that Unit Testing is a waste of time since the unit tests are not being used in production. Some might think it is tough to write unit tests. These notions are wrong.

 

Unit testing is faster

Although initially, it feels like it will take a lot of time for writing code with unit tests, once you get used to it, you can feel the power of it. It regular development setup, for testing any change we need to launch the application, enter the inputs manually and test the application. This can take anywhere between 1 to 5 minutes. Whereas unit tests are executed within a few seconds.

 

Mock expensive resources

Using Mocks we can make Unit testing even faster when the testing involves expensive resources like databases or third-party APIs. Mocks are a powerful way of separating the concerns and ensure your code is working as expected.

 

Unit testing gives you confidence

The most significant advantage of unit testing is when you are making enhancements to an already existing application. Without fully covered unit test cases, it becomes risky to make changes to the code. With unit test cases, you can add your test cases to the existing set and ensure all of them pass. This way you can be confident that none of the previous functionality breaks because of your changes.

 

Build Process

You can integrate executing the test cases as part of the build process. This ensures that every commit or change is tested so that none of the previous functionality is broken. This can be completely automated so that the test cases are executed for every change.

 

How to do Unit Testing

To know more details and to get started with unit testing please see the famous article by Joshua Hayden at https://www.toptal.com/java/getting-started-with-junit

Leave a Reply

Your email address will not be published. Required fields are marked *