Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

When dealing with legacy code that would be difficult to unit test, consider writing component tests instead. Those should be easier to write and make it easier and while still making it safe to refactor or change the code as needed.

Smells

Tests Difficult to Name

Too Many Dependencies

...

A test that is difficult to name usually indicates that it is trying to verify multiple behaviors and should be broken up. It may also indicate that the class under test is doing too much and is breaking the Single Responsibility Principle and may need to be refactored.

Too Many Mocks

A test that requires many mocks usually indicates that the class under test is breaking the Single Responsibility Principle or the Law of Demeter and may need to be refactored.

Dependencies Difficult to Mock

Classes that make it difficult to mock their dependencies usually need to be refactored to follow the Dependency Inversion Principle and use dependency injection. For cases where the class to be mocked is instantiated inside the class multiple times, consider creating and injecting a factory class or a Supplier, or use the Factory Method design pattern.

Tests Need to be Run in a Specific Order

...