...
Like component tests, a class' external dependencies are usually mocked out so that downstream failures can be properly tested. There are however cases when using a real dependency is acceptable. However, that approach should be the exception rather than the rule and should not cause the tests to take longer to run and go against any of theĀ Unit Testing BestPractices.
Test Types Comparison
Unit | Component | Integration/end-to-end | |
---|---|---|---|
Type | White box | Gray box | Black box |
Scope | Single class | Multiple classes making up a service or components; usually contained in a single maven module | Application as a whole |
Coverage | All methods, input/output combinations, conditionals, exceptions, etc.; aim for 100% | Service or component external interfaces, including input/output and errors combinations | Application installation, configurations and deployments; interactions with other applications and external systems |
Mocking | All external classes | External components and services, but usually not embedded libraries | External systems not part of the code base |
Time to run | Seconds | Few minutes | Minutes to hours |
Frequency | Every build; when class changed | Every build; when component/service or one of its dependencies changed | Nightly to a few times a day - unless there's a way to know what tests to run based on the changes made |
Strategy
The overall strategy is to achieve a better balance between end to end, component and unit tests. Over time, the distribution of tests should shift towards the test pyramid, with 70% unit tests, 20% component tests and 10% end to end tests.
...