DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Testing Private Methods in the Java World

Testing Private Methods in the Java World

Axel Fontaine user avatar by
Axel Fontaine
·
Jul. 18, 12 · Interview
Like (0)
Save
Tweet
Share
7.36K Views

Join the DZone community and get the full member experience.

Join For Free
Testing private methods is something that has traditionally drawn heated debates in the Java world.

The solutions usually fall into 4 categories:
  • Don't test private methods
  • Use reflection
  • Use a nested class
  • Change the visibility

Let's look at them in turn:

Don't test private methods

This really leaves us with three choices:
  • refactor to make the method public in some helper class
  • test through a calling method with a higher visibility
  • give up
A delightful choice between increased bloat, higher test complexity and resignation!

No, thank you!


Use reflection

Why make things simple when you can also make them hard and long-winded? Haven't you always been dreaming of needing 10 lines of code to make a method call? (Yes, there are now even entire tools dedicated to this big operation!) Extra bonus points because you can now also prevent your IDE's compiler and refactoring tools from helping you!

No, thank you!


Use a nested class

No too bad, but with 3 significant drawbacks:
  • no separate sources / test sources folders
  • larger classes
  • unit test code in production binaries
We can do better.

No thank you!


Change the visibility

This leaves us with the last option. Not perfect either, but by far the most pragmatic!

It trades a slight increase in visibility (to package-protected) for greatly simplified calling (a regular method call, no less!), while still preserving the valuable sources / test sources separation.

And with a simple documentation habit, it becomes clear to everyone why this design trade-off was made:
?
/* private -> testing */ void myMethodUnderTest() {
    ...
}
So the next time you face this problem, choose the pragmatic route. And enjoy a coffee while everyone else is still trying to complicate things. :-)
 
unit test Java (programming language)

Published at DZone with permission of Axel Fontaine, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Load Balancing Pattern
  • The Importance of Delegation in Management Teams
  • Beginners’ Guide to Run a Linux Server Securely
  • Why Open Source Is Much More Than Just a Free Tier

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: