3

I have a utility class with a private constructor that throws an Illegal state exception and I want to test that it does this.

I've tried a test where I implement the constructor, but since the constructor is private it can't be accessed outside of the class. So is it just pointless to test the constructor?

from the class

 private UtilityClass(){
        throw new IllegalStateException("Utility Class");
    }

from the test class

private UtilityClass = utilityClass;
    @Test(expected = IllegalStateException.class)
    public void constructorTest(){
        utilityClass = new UtilityClass();
    }
nolanite1
  • 153
  • 1
  • 2
  • 10

1 Answers1

0

You can test that a constructor throws an exception by defining a public constructor and then writing appropriate test code. If you have a private constructor (or private method), you cannot directly test it.

Kaan
  • 5,434
  • 3
  • 19
  • 41