如何处理junit中的异常 [英] how to handle exceptions in junit

查看:137
本文介绍了如何处理junit中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些测试用例来测试一些方法。但有些方法抛出异常。我正在做正确的吗?

  private void testNumber(String word,int number){
try {
assertEquals(word,service.convert(number));
} catch(OutOfRangeNumberException e){
Assert.fail(Test failed:+ e.getMessage());
}
}

@Test
public final void testZero(){
testNumber(zero,0);
}

如果我通过 -45 ,它将以 OutOfRangeException 失败,但是我无法测试特定的异常,如 @Test(Expected ...)

解决方案

一个意外的异常是测试失败,所以你不需要也不想抓住一个。

  @Test 
public void canConvertStringsToDecimals(){
String str =1.234;
Assert.assertEquals(1.234,service.convert(str),1.0e-4);
}

直到服务不要抛出一个 IllegalArgumentException ,因为 str 中有一个小数点,这将是一个简单的测试失败。 >

预期的异常应由可选的预期参数 @Test

  @Test(expected = NullPointerException.class)
public void cannotConvertNulls(){
service.convert(NULL);
}

如果程序员懒惰并投掷异常,或者如果他有服务返回 0.0 ,测试将失败。只有一个 NPE 将成功。请注意,预期异常的子类也可以工作。对于 NPE 而言很罕见,但与 IOException SQLException s。



在您想要测试特定异常消息的罕见情况下,您可以使用新的 ExpectedException JUnit @Rule

  @Rule 
public ExpectedException throwwn = ExpectedException.none();
@Test
public void messageIncludesErrantTemperature(){
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage( - 400); //测试消息包含-400。
temperatureGauge.setTemperature(-400);
}

现在,除非setTemperature引发了一个 IAE ,该消息包含用户尝试设置的温度,测试失败。这个规则可以用更复杂的方式使用。






您的示例最好由以下方式处理:

  private void testNumber(String word,int number)
throws OutOfRangeNumberException {
assertEquals(word,service.convert(number)) ;
}

@Test
public final void testZero()
throws OutOfRangeNumberException {
testNumber(zero,0);
}

您可以内联 testNumber ;现在,这并没有什么帮助。您可以将其转换为参数化测试类。


I wrote some test cases to test some method. But some methods throw an exception. Am I doing it correctly?

private void testNumber(String word, int number) {
    try {
        assertEquals(word,  service.convert(number));
    } catch (OutOfRangeNumberException e) {
        Assert.fail("Test failed : " + e.getMessage());
    }
}

@Test
public final void testZero() {
    testNumber("zero", 0);
}

If I pass -45, it will fail with OutOfRangeException but I am not able to test specific exception like @Test(Expected...)

解决方案

An unexpected exception is a test failure, so you neither need nor want to catch one.

@Test
public void canConvertStringsToDecimals() {
    String str = "1.234";
    Assert.assertEquals(1.234, service.convert(str), 1.0e-4);
}

Until service does not throw an IllegalArgumentException because str has a decimal point in it, that will be a simple test failure.

An expected exception should be handled by the optional expected argument of @Test.

@Test(expected=NullPointerException.class)
public void cannotConvertNulls() {
    service.convert(null);
}

If the programmer was lazy and threw Exception, or if he had service return 0.0, the test will fail. Only an NPE will succeed. Note that subclasses of the expected exception also work. That's rare for NPEs, but common with IOExceptions and SQLExceptions.

In the rare case that you want to test for a specific exception message, you use the newish ExpectedException JUnit @Rule.

@Rule
public ExpectedException thrown= ExpectedException.none();
@Test
public void messageIncludesErrantTemperature() {
    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("-400"); // Tests that the message contains -400.
    temperatureGauge.setTemperature(-400);
}

Now, unless the setTemperature throws an IAE and the message contains the temperature the user was trying to set, the test fails. This rule can be used in more sophisticated ways.


Your example can best be handled by:

private void testNumber(String word, int number)
        throws OutOfRangeNumberException {
    assertEquals(word,  service.convert(number));
}

@Test
public final void testZero()
        throws OutOfRangeNumberException {
    testNumber("zero", 0);
}

You can inline testNumber; now, it does not help much. You can turn this into a parametrized test class.

这篇关于如何处理junit中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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