本文介绍了Junit 4.12发出测试异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的方法来尝试获取一些文件。我想测试文件何时不存在,这是我的问题开始的地方。测试不成功。



该方法类似于:

  public Configuration populateConfigs(Configuration config)throws UnRetriableException {

try {
....

} catch(IOException | ConfigurationException e){
log .error(getConfiguration:,e);
抛出新的UnRetriableException(e);
}

抛出新的UnRetriableException(获取配置文件的问题);
}

在我的测试中,我尝试了两个单独的解决方案,没有成功。


  1. 使用

      @Rule 
    public ExpectedException异常= ExpectedException.none();
    @Test
    public void testPopulateConfigurationMissing()throws Exception {

    exception.expect(UnRetriableException.class);
    DefaultConfigHandler configurationFactory = new DefaultConfigHandler(testDirectory,testFileThatIsNonExistant);
    配置配置= configurationFactory.populateConfiguration(systemConfig);

    }


  2. 对于方法二,知道异常被测试。

      @Test(expected = UnRetriableException.class)
    public void testPopulateConfigurationMissing()throws Exception {

    DefaultConfigHandler configurationFactory = new DefaultConfigHandler(testDirectory,testFileThatIsNonExistant);
    配置配置= configurationFactory.populateConfiguration(systemConfig);

    }


实际抛出异常,如下所示:

  com.caricah.iotracah.exceptions.UnRetriableException:java.nio.file.NoSuchFileException在com.caricah.iotracah.system.handler.impl.DefaultConfigHandler.populateConfiguration(DefaultConfigHandler.java:137)上的world / over 
$ 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)
在org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
在org.junit.runners.Suite.runChild( Suite.java:128)
在org.junit.runners.Suite.runChild(Suite.java:27)
在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)
在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)
在org.junit.runners.ParentRunner.runChildren(ParentRunner.java :288)
在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)
在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)
在org.junit.runners.ParentRunner.run(ParentRunner.java:363)
在org.junit.runner.JUnitCore.run(JUnitCore.java:137)

所以我的问题,还有什么需要做的,让测试通过?



解决方案

只是测试它,按预期工作...类... 。

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

下载您的项目(见评论)后,我不知道为什么,但我知道什么的问题是:扩展Testcase 。我认为这样会导致执行单元测试的不同方法(stacktrace意味着它们将被执行,然后使用 JUnit38ClassRunner 执行)。删除它(你不需要它),而是用 Assert。< something> 调用你的断言,例如 Assert.assertTrue(。 ..)。 (您也可以使用静态导入,因此您不必编写Assert部分)。这解决了你的问题,所有的测试都成功了。



另一种可能性似乎是保持扩展TestCase 并使用 @RunWith(BlockJUnit4ClassRunner.class),这也解决了你的问题,所以可能默认的TestCase的Runner不能达到。


I have a simple method trying to fetch some file. I would like to test when the file is none existent and this is where my problem begins. The test keeps failing.

The method is something like :

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

In my tests I have tried two separate solutions without success.

  1. Using the new style as suggested in the 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. For method two which is just how I used to know exceptions are tested.

    @Test(expected = UnRetriableException.class)
    public void testPopulateConfigurationMissing() throws Exception {
    
        DefaultConfigHandler configurationFactory = new  DefaultConfigHandler(testDirectory, testFileThatIsNonExistant);
        Configuration configuration = configurationFactory.populateConfiguration(systemConfig);
    
    }
    

The exception is actually thrown as shown below :

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)

Hence my question, what else do I need to do to make the test pass?

Ofcourse without using the junit3 way of catching the exception.

解决方案

Just tested it, works as expected... The class...

public class SomeClass {

    public void someMethod(String someParameter) throws SomeException {
        throw new SomeException("Yep, really a SomeException");
    }

}

The exception...

public class SomeException extends Exception {

    public SomeException(String message) {
        super(message);
    }

}

And the test class, both tests work exactly as expected:

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

After downloading your project (see comment), I don't know for sure why, but I know what the problem is: It's the extends Testcase. I assume that this somehow leads to a different way of executing your unit tests (stacktrace implies that they get executed with the JUnit38ClassRunner then). Remove it (you don't need it anyway) and instead call your asserts with Assert.<something>, for example Assert.assertTrue(...). (You can use static imports for that, too, so you don't have to write the Assert part). That solves your problem and all tests succeed.

Another possibility seems to be to keep the extends TestCase and use @RunWith(BlockJUnit4ClassRunner.class), which also fixes your problem, so probably the default Runner for a TestCase isn't up to it.

这篇关于Junit 4.12发出测试异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:02
查看更多