Junit 4.12问题测试exception
我有一个简单的方法试图获取一些文件。 我想测试文件何时不存在,这就是我的问题开始的地方。 测试一直在失败。
方法类似于:
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."); }
在我的测试中,我尝试了两个单独的解决方
-
使用SO解决方案中建议的新样式
@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); }
-
对于方法二,这就是我以前如何知道exception的测试。
@Test(expected = UnRetriableException.class) public void testPopulateConfigurationMissing() throws Exception { DefaultConfigHandler configurationFactory = new DefaultConfigHandler(testDirectory, testFileThatIsNonExistant); Configuration configuration = configurationFactory.populateConfiguration(systemConfig); }
实际抛出exception,如下所示:
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方式捕获exception。
刚试过它,按预期工作……class级……
public class SomeClass { public void someMethod(String someParameter) throws SomeException { throw new SomeException("Yep, really a SomeException"); } }
例外……
public class SomeException extends Exception { public SomeException(String message) { super(message); } }
在测试类中,两个测试都完全按预期工作:
public class TestSomeClass { @Rule public ExpectedException exception = ExpectedException.none(); @Test public void testSomeMethodWithRule() throws SomeException { exception.expect(SomeException.class); new SomeClass().someMethod("something"); } @Test(expected=SomeException.class) public void testSomeMethodWithExpected() throws SomeException { new SomeClass().someMethod("something"); } }
下载你的项目后(见评论),我不知道为什么 ,但我知道问题是什么 :它是extends Testcase
。 我假设这会以某种方式导致执行unit testing的不同方式(stacktrace意味着它们随后会被JUnit38ClassRunner
执行)。 删除它(你无论如何都不需要它)而是用Assert.
调用你的断言,例如Assert.assertTrue(...)
。 (您也可以使用静态导入,因此您不必编写Assert部分)。 这解决了您的问题,所有测试都成功。
另一种可能性似乎是保留extends TestCase
并使用@RunWith(BlockJUnit4ClassRunner.class)
,这也解决了你的问题,所以可能是TestCase的默认Runner不符合它。