Search code examples
javatestingtest-double

Is it ok to place test double implementations on the main java code


With regards to test doubles is it ok to place them on the main code so that they can be used, when testing code that contains the original library jar as a dependency?

There is code that assists me on unit testing (test doubles) on a library I implemented. Is it ok to keep this code in the jar so that it can be used by code that uses the library as a dependency.


Solution

  • The short answer is no.

    As you are referring to Java I may point to Maven dependency sopes and Gradle configurations. They separate dependencies between your main code and those purely for test purposes. And they exist for a reason. You may use production ready libraries for your test, but you must not deploy test doubles to your production system.

    I trust your test doubles comply with your style guide and are tested themselves. But what about security requirements? Are you really sure you do not introduce vulnerabilities into your system with theses test doubles? Do you really want to consider these unnecessary parts of your system while planning your penetration test? Or worse, do you want to explain your pen test specialist to consider them?

    If you developed reusable test components you did something useful. But you have to isolate them in separate libraries. They are part of a special purpose test framework. Treat them as you would treat JUnit.