This question already has an answer here:
Sometimes, especially when working with third party code, I write unit-test specific code in my production code. This happens when third party code uses singletons, relies on constants, accesses the file-system/a resource I don't want to access in a test situation, or overuses inheritance. The form my unit-test specific code takes is usually the following:
if (accessing or importing a certain resource fails) I assume this is a test case and load a mock object
Is this poor form, and if it is, what is normally done when writing tests for code that uses untestable third party code?