Junit 4.12 Выпуск тестирования исключения
У меня есть простой метод, пытаясь получить какой-то файл. Я хотел бы проверить, когда файла не существует, и именно здесь начинается моя проблема. Тест продолжает проваливаться.
Метод что-то вроде:
public Configuration populateConfigs(Configuration config) throws UnRetriableException {
try {
....
} catch (IOException | ConfigurationException e) {
log.error(" getConfiguration : ", e);
throw new UnRetriableException(e);
}
throw new UnRetriableException("problem getting config files.");
}
В своих тестах я попробовал два отдельных решения без успеха.
Используя новый стиль, предложенный вТАК решение
@Rule
public ExpectedException exception = ExpectedException.none();
@Test
public void testPopulateConfigurationMissing() throws Exception {
exception.expect(UnRetriableException.class);
DefaultConfigHandler configurationFactory = new DefaultConfigHandler(testDirectory, testFileThatIsNonExistant);
Configuration configuration = configurationFactory.populateConfiguration(systemConfig);
}
Для второго метода, как я привык знать, тестируются исключения.
@Test(expected = UnRetriableException.class)
public void testPopulateConfigurationMissing() throws Exception {
DefaultConfigHandler configurationFactory = new DefaultConfigHandler(testDirectory, testFileThatIsNonExistant);
Configuration configuration = configurationFactory.populateConfiguration(systemConfig);
}
Исключение фактически выдается, как показано ниже:
com.caricah.iotracah.exceptions.UnRetriableException: java.nio.file.NoSuchFileException: world/over
at com.caricah.iotracah.system.handler.impl.DefaultConfigHandler.populateConfiguration(DefaultConfigHandler.java:137),
at com.caricah.iotracah.system.handler.impl.DefaultConfigHandlerTest.testPopulateConfigurationMissingDirectory(DefaultConfigHandlerTest.java:137)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
Отсюда мой вопрос, что еще мне нужно сделать, чтобы пройти тест?
Конечно, не используя junit3 способ перехвата исключения.