vendredi 18 janvier 2019

How can I test my private constructors that throws an exception

I would like to know how can I test my private constructors that throws an IllegalStateException, I have search and found something like this:

@Test
public void privateConstructorTest()throws Exception{
    Constructor<DetailRecord> constructor = DetailRecord.class.getDeclaredConstructor();
    assertTrue(Modifier.isPrivate(constructor.getModifiers()));
    constructor.setAccessible(true);
    constructor.newInstance();
}

and this is the constructor:

private DetailRecord(){
    throw new IllegalStateException(ExceptionCodes.FACTORY_CLASS.getMessage());
}

the test works if the constructors doesnt throw an exception

Aucun commentaire:

Enregistrer un commentaire