Skip to main content
Commonmark migration
Source Link

<rant "I think I heard enough whining. Guess it's about time to say loud and clear..."> #Private methods are beneficial to unit testing.

Private methods are beneficial to unit testing.

###What do I gain from keeping it private

What do I gain from keeping it private

###What do I loose from weakening access limitation

What do I loose from weakening access limitation

<rant "I think I heard enough whining. Guess it's about time to say loud and clear..."> #Private methods are beneficial to unit testing.

###What do I gain from keeping it private

###What do I loose from weakening access limitation

<rant "I think I heard enough whining. Guess it's about time to say loud and clear...">

Private methods are beneficial to unit testing.

What do I gain from keeping it private

What do I loose from weakening access limitation

replaced http://programmers.stackexchange.com/ with https://softwareengineering.stackexchange.com/
Source Link

Generally, one would better be judicious about using protected access at all. The reasons for that are laid out in answers to prior questions over here: Why is Clean Code suggesting avoiding protected variables?Why is Clean Code suggesting avoiding protected variables?

I wrote an explanation for this in an answer to prior questionanswer to prior question as a side note because that other question and answer were focused on different matters. But for the question you asked, it seems to apply fully and directly, so I will simply copy that part over here.

  1. New test ensures that expected behavior for this usage won't change without a notice since if it changes, test will fail.

  2. An outside reader may look into this test and learnlearn how it is supposed to use and behave (here, outside reader includes my future self, since I tend to forget the code a month or two after I'm done with it).

  3. New test is tolerant to refactoring (do I refactor private methods? you bet!) Whatever I do to privateMethod, I'll always want to test nonPrivateMethod(true). No matter what I do to privateMethod, there will be no need to modify test because method isn't directly invoked.

Generally, one would better be judicious about using protected access at all. The reasons for that are laid out in answers to prior questions over here: Why is Clean Code suggesting avoiding protected variables?

I wrote an explanation for this in an answer to prior question as a side note because that other question and answer were focused on different matters. But for the question you asked, it seems to apply fully and directly, so I will simply copy that part over here.

  1. New test ensures that expected behavior for this usage won't change without a notice since if it changes, test will fail.

  2. An outside reader may look into this test and learn how it is supposed to use and behave (here, outside reader includes my future self, since I tend to forget the code a month or two after I'm done with it).

  3. New test is tolerant to refactoring (do I refactor private methods? you bet!) Whatever I do to privateMethod, I'll always want to test nonPrivateMethod(true). No matter what I do to privateMethod, there will be no need to modify test because method isn't directly invoked.

Generally, one would better be judicious about using protected access at all. The reasons for that are laid out in answers to prior questions over here: Why is Clean Code suggesting avoiding protected variables?

I wrote an explanation for this in an answer to prior question as a side note because that other question and answer were focused on different matters. But for the question you asked, it seems to apply fully and directly, so I will simply copy that part over here.

  1. New test ensures that expected behavior for this usage won't change without a notice since if it changes, test will fail.

  2. An outside reader may look into this test and learn how it is supposed to use and behave (here, outside reader includes my future self, since I tend to forget the code a month or two after I'm done with it).

  3. New test is tolerant to refactoring (do I refactor private methods? you bet!) Whatever I do to privateMethod, I'll always want to test nonPrivateMethod(true). No matter what I do to privateMethod, there will be no need to modify test because method isn't directly invoked.

http://programmers.stackexchange.com/questions/292087/testing-private-methods-as-protected/292090?noredirect=1#comment605673_292090
Source Link
gnat
  • 20.9k
  • 29
  • 115
  • 295

There could be many cases and reasons when one needs to redesign code for testability. Various difficulties in writing unit tests often serve as good indication for such a need. But, "oh, that method is private" is not one of those indicative difficulties.

I wrote an explanation for this in an answer to prior question as a side note because that other question and answer were focused on different matters. But for the question you asked, it seems to apply fully and directly, so I will simply copy that part over here.

I wrote an explanation for this in an answer to prior question as a side note because that other question and answer were focused on different matters. But for the question you asked, it seems to apply fully and directly, so I will simply copy that part over here.

There could be many cases and reasons when one needs to redesign code for testability. Various difficulties in writing unit tests often serve as good indication for such a need. But, "oh, that method is private" is not one of those indicative difficulties.

I wrote an explanation for this in an answer to prior question as a side note because that other question and answer were focused on different matters. But for the question you asked, it seems to apply fully and directly, so I will simply copy that part over here.

Source Link
gnat
  • 20.9k
  • 29
  • 115
  • 295
Loading