Добавьте необязательный ожидаемый атрибут к аннотации @ Test .Следуя тесту, который проходит, когда ожидается ожидаемое IllegalStateException :
@Test(expected=IllegalStateException.class)
public void privateConstructorTest() {
Constructor<DetailRecord> constructor = DetailRecord.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}
Или вы можете перехватить исключение и проверить его следующим образом:
@Test
public void privateConstructorTest() {
Constructor<DetailRecord> constructor = DetailRecord.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
Throwable currentException = null;
try {
constructor.newInstance();
catch (IllegalStateException exception) {
currentException = exception;
}
assertTrue(currentException instanceof IllegalStateException);
}