What is Unit Testing?

Through unit testing, we can define the intended behavior of the code and validate that it indeed performs as expected. Unit testing has evolved to become a fascinating junction of design, programming, and debugging.

Unit testing involves writing test code to verify the functionality of other code units. Although the size of a unit is not precisely defined, we can consider a unit to be a small code segment that performs a useful action in our system. Typically, a unit alone does not represent a complete end-to-end behavior, but rather a small portion of it.

Scenarios where JUnit and unit testing is used

  • After implementing a new feature, we write unit tests to verify its expected behavior.
  • To document changes made in the system and help other developers understand the rationale behind the implementation, we write unit tests.
  • Before making changes to the existing code, we write unit tests to ensure that the upcoming modifications don't break the existing behavior.
  • We write unit tests to understand the current behavior of the system.
  • We also write unit tests to identify when third-party code stops working as intended.

Benefits of Unit testing

Learning how to implement unit testing can improve considerably the quality of the code we produce. Not only that, as programmers it will increase our range of skills.

If we approach unit testing with an open mind, it might even change the way we build code.`

In addition, unit testing increases the confidence in our code, even though we still need integration and/or acceptance tests to verify end-to-end behaviour.