How can use a private inner class for testing?

2029 views java

For example :

I have a private enum in my class, like this:

 private enum MyEnum {


    private final String x;
    private final String y;

    MyEnum(String x, String y) {
        this.x= x;
        this.y= y;

    public String getX() {
        return x;

    public String getY() {
        return y;

I want to use this type in test class, without redefining it again.

Also, I want to keep minimum visibility.

answered question

Make it package private and put your test in same package.

1 Answer


It is not possible to test a private class. You have to increase visibility to at least "package-private" by simply removing the "private" access modifier.

The standard way on how to test such class is to define the test in exactly the same package as tested class.

The tested method itself also has to be at least "package-private". It is possible to test private methods too, but that requires usage of reflection and it is rather considered a hack (

posted this

Have an answer?


Please login first before posting an answer.