Finding Design Flaws with Unit Testing
Explore how unit testing provides crucial feedback on software design by identifying code smells during the arrange, act, and assert steps. Understand common design issues exposed by tests and learn the limitations of unit tests and code coverage. This lesson helps you use test-driven development to detect and address faulty designs early, enhancing code robustness and collaboration with QA.
We'll cover the following...
Our tests are a rich source of feedback on our design. As we make decisions, we write them as test code. Seeing this code—the first usage of our production code—brings into sharp focus how good our proposed design is. When our design isn’t good, the AAA sections of our test will reveal those design issues as code smells in the test. Let’s try to understand in detail how each of these can help identify a faulty design.
Arrange
If the code in our Arrange step is messy, our object might be difficult to create and configure. It might need too many parameters in a constructor or too many optional parameters left as null in the test.
It can be that the object needs too many dependencies injected, indicating that it has too many responsibilities or it might need too many primitive data parameters to pass in a lot of configuration items.
These signal that how we create our object might benefit from a redesign.
Act
Calling the main part of the code in the Act step is usually straightforward, but it can reveal some basic design errors. For example, we might have unclear parameters that we pass in, signatures such as a list of Boolean or ...