【问题标题】:Junit 4.12 Issue testing exceptionJunit 4.12 问题测试异常
【发布时间】:2015-08-09 12:46:35
【问题描述】:

我有一个简单的方法试图获取一些文件。我想测试文件何时不存在,这就是我的问题开始的地方。测试一直失败。

方法类似于:

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.");
}

在我的测试中,我尝试了两种不同的解决方案,但均未成功。

  1. 使用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);
    
    }
    
  2. 对于方法二,这正是我过去知道异常被测试的方式。

    @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捕获异常的方式。

【问题讨论】:

    标签: java exception junit junit4


    【解决方案1】:

    刚刚测试过,按预期工作...类...

    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。我假设这会以某种方式导致执行单元测试的不同方式(stacktrace 暗示它们会使用 JUnit38ClassRunner 然后执行)。删除它(无论如何你都不需要它),而是用Assert.<something> 调用你的断言,例如Assert.assertTrue(...)。 (您也可以为此使用静态导入,因此您不必编写 Assert 部分)。这样就解决了您的问题,并且所有测试都成功了。

    另一种可能性似乎是保留extends TestCase 并使用@RunWith(BlockJUnit4ClassRunner.class),这也解决了您的问题,因此可能TestCase 的默认Runner 不适合它。

    【讨论】:

    • 如果你没问题,请查看 github 上的测试:github.com/caricah/iotracah/blob/master/src/test/java/com/… 失败的构建也在这里:travis-ci.org/caricah/iotracah/builds/74790700 也许你可以看到我没有看到的内容。
    • 好的,下载了你的项目,老实说,我不知道为什么,但我知道问题所在:它是“扩展测试用例”。我认为这会以某种方式导致执行单元测试的不同方式。删除它(无论如何你都不需要它),而是用Assert.<something> 调用你的断言,例如Assert.assertTrue(...)。 (您也可以为此使用静态导入,因此您不必编写 Assert 部分)。这样就解决了您的问题,并且所有测试都成功了。
    • 另一种可能性似乎是保留extends TestCase 并使用@RunWith(BlockJUnit4ClassRunner.class),这也解决了您的问题,因此可能TestCase 的默认Runner 不适合它。
    • 谢谢伙计。这实际上解决了我的问题。你应该写一个答案,这样我才能接受。
    • 编辑了答案,这样人们就不必在 cmets 中搜索了。还添加了这样一个事实,即堆栈跟踪暗示extends TestCase 似乎导致它与 38 Runner 一起执行。
    猜你喜欢
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    相关资源
    最近更新 更多