All posts
How to test private methods?

How to test private methods?

From time to time I have had to face this question: how to test private methods? I have put together in an article the techniques that I usually use.

blog-cover

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.

jkscroll JKposts Ssearch Dtheme Llang Ttoc Ccopy ?help

Keyboard Shortcuts

Navigation

HHome
BBlog
RReadings
PTopics
EServices
ATalks
LToggle language

Actions

SSearch
DToggle theme
CCopy URL
TToggle TOC

Scroll

jScroll down
kScroll up
GScroll to bottom
ggScroll to top

Posts

JNewer post
KOlder post

General

?Show this help
ShiftHold to show hints
EscClose