Introduction to unit testing
Unit testing is defined as testing a block of code in isolation. A class can have multiple methods. And, thus, there could be multiple unit tests for a class. The following are some of the best practices that need to be followed when working with unit tests:
- Refactor code: A method that is long and complex enough, or rather, does multiple things, or has multiple responsibilities that need to be refactored. In other words, a method with a very high cyclomatic complexity would need to be refactored to small methods that conform to the single responsibility principle (a block of code will have just one reason to change).
- Using test doubles: As the unit tests are about testing a block of code in isolation, any dependencies from the code would need to be mocked. In other words, it will be necessary to make use of test doubles instead of actual service or components when writing unit tests. This can be achieved using some of the following concepts related to mocking...