Junit 4.12 问题测试异常
Junit 4.12 Issue testing exception
我有一个简单的方法来尝试获取一些文件。我想测试文件何时 none 存在,这就是我的问题开始的地方。测试一直失败。
方法类似于:
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 solution
中建议的新样式
@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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
因此我的问题是,我还需要做什么才能通过测试?
当然不用junit3的异常捕获方式了
刚刚测试过,按预期工作...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);
}
}
和测试 class,两个测试都按预期工作:
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
。我假设这会以某种方式导致执行单元测试的不同方式(stacktrace 意味着它们是用 JUnit38ClassRunner
执行的)。删除它(反正你不需要它),而是用 Assert.<something>
调用你的断言,例如 Assert.assertTrue(...)
。 (您也可以为此使用静态导入,因此您不必编写 Assert 部分)。这解决了你的问题,所有测试都成功了。
另一种可能性似乎是保留 extends TestCase
并使用 @RunWith(BlockJUnit4ClassRunner.class)
,这也解决了您的问题,因此 TestCase 的默认 Runner 可能不适合它。
我有一个简单的方法来尝试获取一些文件。我想测试文件何时 none 存在,这就是我的问题开始的地方。测试一直失败。
方法类似于:
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 solution
中建议的新样式@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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
因此我的问题是,我还需要做什么才能通过测试?
当然不用junit3的异常捕获方式了
刚刚测试过,按预期工作...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);
}
}
和测试 class,两个测试都按预期工作:
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
。我假设这会以某种方式导致执行单元测试的不同方式(stacktrace 意味着它们是用 JUnit38ClassRunner
执行的)。删除它(反正你不需要它),而是用 Assert.<something>
调用你的断言,例如 Assert.assertTrue(...)
。 (您也可以为此使用静态导入,因此您不必编写 Assert 部分)。这解决了你的问题,所有测试都成功了。
另一种可能性似乎是保留 extends TestCase
并使用 @RunWith(BlockJUnit4ClassRunner.class)
,这也解决了您的问题,因此 TestCase 的默认 Runner 可能不适合它。