Blog Archives
Test Driven Development – Clean Code Video
- Design and Code rots lead to fragile, rigit and in-mobile code base. Debugging becomes difficult, estimates mount and implementing any new changes becomes difficult. Uncertainity in the code increases.
- Maintaining a test suite with a high code coverage has below benefits
1. Keeps defects under control
2. Gives us the courage to change the code in order to clean it
3. Unit tests act as code examples for your API. They are low level design documents. For e.g. The test cases helps us to know all the possible ways to call a particular API.
4. Writing tests first, makes the production code testable. To write unit tests you would have to make your functions decoupled from each other. This leads to an improved design.
Different Singleton instances with JUnit tests
Spring provides a test annotation called @DirtiesContext which is very helpful when testing singleton beans. When added at the class level with the class mode set to AFTER_EACH_TEST_METHOD, spring creates a new application context for every test case (method). This annotation comes to your rescue when you have some state in your singleton class.