Assume there's some code that's already in production that needs some unit testing. Generally speaking, would you want to refactor this code that's already in production by adding things like adding wrappers, or add in a testing framework like PowerMock? I've heard it's bad practice to use powermock, but it seems unreasonable to modify code in production just for the sake of testing it.

  • -1 for assuming everyone knows you are talking of Java.
    – Doc Brown
    Jun 21, 2020 at 19:45
  • @DocBrown Does it matter if it's java or not? Jun 21, 2020 at 20:02
  • @Goldname It matters a bit. In something like Python, dynamically patching the dependencies is pretty much the preferred way of unit testing. Jun 21, 2020 at 21:27

1 Answer 1


You need to do the risk-benefit analysis of each approach.

  • Why does this code "need" some unit testing? What actual benefit is this going to give?
  • How much risk is it to refactor it to be unit testable in a "standard" fashion?
  • How much risk is it to use PowerMock?

We can't answer any of those for you, because we don't know your code.

Personally, even when working with the cruftiest codebases, I have found very few that cannot be incrementally refactored to introduce unit tests - I strongly recommend Working Effectively with Legacy Code as a toolbox of techniques for getting unit tests into an existing codebase.

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.