All posts

How to Test Private Methods?

Testing private methods. When and how?

How to Test Private Methods?
Do not test private methods. If one is complex enough to deserve its own test, extract it into a class and test that behaviour instead.

This is a question that I have encountered with some frequency for a long time. So I thought I would put together my thoughts on the subject here.

Short answer #

Never.

Long answer #

Never ever.


What if…? #

If you really want to test a private method, consider extracting that private method logic into a separate class, and write a unit test for that class’ behavior.

For this one, I was inspired by Fran Iglesias’ original post.

Keyboard Shortcuts

Movement vim hjkl

hPrevious post← left
jScroll down↓ down
kScroll up↑ up
lNext post→ right
ggScroll to top
GScroll to bottom
nNext sectionnext heading
NPrevious sectionprevious heading

Go to g = go

ghHomego home
gbBloggo blog
grReadingsgo readings
gcCVgo cv

Actions

/⌘KSearchvim search
dToggle themedark mode
tToggle TOCtable of contents
iSwitch languagei18n
mToggle highlightmark text

General

?Show this help
EscClose
:Terminalvim command mode
↑↑↓↓←→←→BA???