有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

在我的测试中,我尝试了两种不同的解决方案,但都没有成功

  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捕获异常的方法


共 (1) 个答案

  1. # 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(...)。(也可以使用静态导入,因此不必编写断言部分)。这就解决了你的问题,所有的测试都成功了

    另一种可能性似乎是保留extends TestCase并使用@RunWith(BlockJUnit4ClassRunner.class),这也解决了您的问题,因此测试用例的默认运行程序可能无法胜任