How can use a private inner class for testing?

2029 views java
-3

For example :

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

 private enum MyEnum {

  A(x,y),
  B(x,y);

    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

0

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 (https://stackoverflow.com/a/18533714/859673)

posted this

Have an answer?

JD

Please login first before posting an answer.