2

In larger projects, there can often be a group of tests that test the functionality of related classes. These tests often share a lot of setup and preparation functionality.

What happens then is that a base class is defined for these test cases so that they can share there instance fields and common functionality. This leads to problems when a test uses a lot of functionality present in multiple classes.

Would it be possible to use some form of composition for this type of code reuse?

Let me give a concrete example. I have a few classes that can 'write' a list of transactions, defined by the interface 'TransactionWriter'. Concrete implementations are 'FileTransactionWriter' and 'ConsoleTransactionWriter'.

Both of the test classes for these implementations would need to set up transactions with some arbitrary data. This code would be duplicated in both classes if not extracted to a base test class. But can the same goal be achieved with composition?

2

The trade-off between inheritance and composition is exactly the same for test code as it is for business code.

Composition requires delegating calls for all functionality that you want to project to your own public interface. That means that composition becomes more cumbersome the larger the functionality that you want to reuse.

Inheritance achieves the reuse with no extra effort, but itself leads to bloated public interfaces because it always reuses everything. Also, many languages only allow you to inherit once.

This means that to reuse one specific it of functionality like writing things, inheritance might be a good solution, but it depends too much on the specifics of our code base to give a general answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.