Skip to content

Test publics, not private internals

Public parts of classes, especially the interfaces they implement, are rather stable and unlikely to change. Let your unit tests validate only the publics to make them robust and minimize the effort you have to spend when you refactor the class. Protected and private internals, in contrast, may change very quickly through refactoring, such that each refactoring would needlessly break your tests.

An urgent need to test private or protected methods may be an early warning sign for several kinds of design flaws. Ask yourself:

  • Did you accidentally bury a concept in your class that wants to come out into its own class, with its own dedicated suite of tests?

  • Did you forget to separate the domain logic from the glue code? For example, implementing the domain logic directly in the class that is plugged into BOPF as an action, determination, or validation, or that was generated by SAP Gateway as a *_DPC_EXT data provider, may not the best idea.

  • Are your interfaces too complicated and request too much data that is irrelevant or that cannot be mocked easily?

From SAP/styleguides under CC BY 3.0 — more details
Last updated on