在不同的JUnit测试中管理检查的异常 [英] Managing checked exceptions in different JUnit tests

查看:122
本文介绍了在不同的JUnit测试中管理检查的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的一种方法编写Java单元测试.方法声明是这样的:

I am writing a Java Unit test for one of my method. The method declaration is like this:

public int convertToInteger() throws InvalidRomanNumberException
{
    int result=0;
    BaseRomanNumeral num1, num2; 
    int i=0;
    if(!validOperation())
        throw new InvalidRomanNumberException();
}

现在,我正在尝试编写两个单元测试.一种是测试是否抛出了正确的异常.另一个是确保写入转换发生.这就是我的测试用例的样子

Now I am trying to write two unit tests. One is to test if the right exception is thrown. Another one is to make sure that that the write conversion happens. This is how my test case looks

@Test
public void testRomanNumberConversion() {
    String romanValue="MCMII";
    RomanNumber num=new RomanNumber(romanValue);
    assertEquals(1903,num.convertToInteger());  
}

@Test(expected = InvalidRomanNumberException.class)
public void testInvalidRomanNumberExceptionThrown()  {
    String romanValue="MCMIIII";
    RomanNumber num=new RomanNumber(romanValue);
    num.convertToInteger(); 
}

对于这两个测试用例,我都会收到一条错误消息,指出未处理的InvalidRomanNumberException.仅当我向每个方法定义添加InvalidRomanNumberException时才解决此问题.但是我认为这不是正确的方法.只想和你们其他人一起检查,这里的规范是什么?我应该如何解决此未处理的异常消息

For both these test cases I am getting an error saying Unhandled InvalidRomanNumberException. This is resolved only when I add throws InvalidRomanNumberException to each method definition. But I don't think that is the right way. Just want to check with the rest of you, what is the norm here? How should I resolve this unhandled exception message

推荐答案

由于看起来InvalidRomanNumberException是已检查的异常,因此您必须将其包围在try-catch内,或者声明方法throws InvalidRomanNumberException. JUnit与否,这是常态.

Since it looks like InvalidRomanNumberException is a checked exception, you have to either surround it with a try-catch or declare that the method throws InvalidRomanNumberException. JUnit or not, this is the norm.

话虽这么说,expect会抛出InvalidRomanNumberException的测试用例方法应该理想地声明它为throws一个,因为用try-catch抑制它是没有意义的,因为您的测试用例将失败.另一方面,您希望不会引发异常的测试用例方法可以在convertToInteger方法周围使用try-catch,并且无论是否引发异常,该测试用例都应在预期的对象上具有assertconvertToInteger方法的结果.

That being said, the test case method that you expect will throw a InvalidRomanNumberException should ideally declare that it throws one since there is no point suppressing it with a try-catch as your test case will fail. On the other hand, the test case method that you expect will not throw an exception can use a try-catch around the convertToInteger method and regardless of whether an exception is thrown, this test case should have an assert on the expected result from convertToInteger method.

JUnit测试用例的最终结果应该是测试通过还是失败.运行时发生异常将不会显示任何内容. JUnit测试用例一定不能崩溃.

The end result of a JUnit test case should be whether the test passed or failed. An exception at runtime would indicate neither. A JUnit test case must not crash.

这篇关于在不同的JUnit测试中管理检查的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆