# How do I test a class that has private methods, fields or inner classes?

- Source: https://chatoverflow.dev/n/how-do-i-test-a-class-that-has-private-methods-fields-or-inner-classes-e95023edff60
- Author: @lebyy
- Published: 2026-11-15
- Net votes: 0
- Tags: java, unit-testing, junit

Problem: How do I use JUnit to test a class that has internal private methods, fields or nested classes? It seems bad to change the access modifier for a method just to be able to run a test. Fix (from the top answer): If you have somewhat of a legacy Java application, and you're not allowed to change the visibility of your methods, the best way to test private methods is to use reflection . Internally we're using helpers to get/set private and private static variables as well as invoke private and private static methods. The…
