Java JUnit4.12问题测试异常
我有一个简单的方法试图获取一些文件。我想测试文件何时不存在,这就是我的问题开始的地方。考试一直不及格 该方法类似于:Java JUnit4.12问题测试异常,java,exception,junit,junit4,Java,Exception,Junit,Junit4,我有一个简单的方法试图获取一些文件。我想测试文件何时不存在,这就是我的问题开始的地方。考试一直不及格 该方法类似于: public Configuration populateConfigs(Configuration config) throws UnRetriableException { try { .... } catch (IOException | ConfigurationException e) { log.e
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.");
}
在我的测试中,我尝试了两种不同的解决方案,但都没有成功
@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捕获异常的方法。刚刚测试过,工作正常。。。班级
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");
}
}
下载您的项目(请参阅评论)后,我不确定为什么,但我知道问题是什么:扩展了测试用例。我假设这会以某种方式导致执行单元测试的不同方式(stacktrace意味着它们是通过JUnit38ClassRunner
执行的)。删除它(您无论如何都不需要它),而是使用Assert.
调用您的断言,例如Assert.assertTrue(…)
。(也可以使用静态导入,因此不必编写断言部分)。这就解决了你的问题,所有的测试都成功了
另一种可能是保留扩展测试用例
并使用@RunWith(BlockJUnit4ClassRunner.class)
,这也解决了您的问题,因此测试用例的默认运行程序可能无法胜任 如果你同意的话,请看一下github上的测试:失败的构建也在这里:也许你可以看到我没有看到的东西。好吧,下载了你的项目,说实话,我不知道为什么,但我知道问题:这是“扩展测试用例”。我假设这会以某种方式导致执行单元测试的不同方式。删除它(您无论如何都不需要它),而是使用Assert.
调用您的断言,例如Assert.assertTrue(…)
。(也可以使用静态导入,因此不必编写断言部分)。这就解决了您的问题,所有测试都成功了。另一种可能性似乎是保持扩展测试用例
,并使用@RunWith(BlockJUnit4ClassRunner.class)
,这也解决了您的问题,因此测试用例的默认运行程序可能无法胜任。谢谢。这实际上解决了我的问题。你应该写一个答案,这样我才能接受。编辑答案,这样人们就不必搜索评论了。还添加了一个事实,即stacktrace意味着扩展TestCase
似乎会导致使用38运行程序执行它。
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");
}
}