-1

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.

  • Make it package private and put your test in same package. – talex Nov 09 '18 at 06:47
  • @talex This is the only way ? I don't want to modified the implementation. And, on the other hand, if I use package private this field will be visible in other class, and I don't want that. – Remus Crisan Nov 09 '18 at 07:03
  • Sorry, but there is no way to make invisible thing visible in java, except reflection. – talex Nov 09 '18 at 07:05

1 Answers1

2

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)

ygor
  • 1,726
  • 1
  • 11
  • 23