I have a sealed class with protected methods whose behaviour I want to test. This makes it hard to test directly, and hard to mock.
It's in a codebase that wasn't developed in a TDD manner, and I'm now adding unit tests for specific functionality.
What are the general approaches possible in this case? At the moment I have:
- Have the class unsealed. Then create a proxy or adapter derived from the class in our test code to tunnel access to the protected method.
- Factor out the behaviour in the protected method to a delegate/functor, and re-inject it. Then test the factored out behaviour independently.
- Test by calling the closest public method in the inheritance hierarchy that uses the protected method. Potentially leads to lots of mocking, and exposure to risk when code other than that under test changes -- creating fragile tests.
- Use reflection to get access to the protected method. Then call it directly.
Are there any more?