2014-11-04 80 views
2

你能告诉我请它是正常的做法写方法又名JUnit测试抛出异常,例如junit测试方法可以抛出异常吗?

class A { 
    public String f(int param) throws Exception { 
     if (param == 100500) 
      throw new Exception(); 
     return ""; 
    } 
} 

private A object = new A(); 

@Test 
public void testSomething() throws Exception { 
    String expected = ""; 
    assertEquals(object.f(5), expected); 
} 

事实上,方法f()不会抛出异常的那个参数(5),但仍然我必须声明这个例外。

+5

是的,这是你走的路。而JUnit运行者也会赶上抛出的异常,然后测试就会失败。 – Seelenvirtuose 2014-11-04 14:54:02

+0

@Test(Expected = SomethingDoesn'tWorkException)你可以用它来在JUnit测试中捕获异常 – ha9u63ar 2014-11-04 14:58:29

+1

@hagubear你只希望在预期异常的特定测试用例中使用'expected'。但在所示的示例中,“throws”是必要的,但不期望有例外。 – 2014-11-04 14:59:33

回答

6

是的,它是完全正常的,如果它抛出异常,测试将被视为失败。

即使您知道特定情况没有(此检查由编译器完成),您仍需要指定该方法抛出Exception

在这种情况下,你所期望的是object.f(5)返回一个空字符串。任何其他结果(非空字符串或抛出异常)都会导致测试用例失败。

1

如果您正在调用的方法抛出一个检查异常yes,则需要尝试捕获或重新抛出。从测试本身做到这一点很好。使用JUnit测试Exception有很多种方法。我试图提供一份简短的总结:

import org.junit.Rule; 
import org.junit.Test; 
import org.junit.rules.ExpectedException; 

/** 
* Example uses Kent Beck - Test Driven Development style test naming 
* conventions 
*/ 
public class StackOverflowExample { 

    @Rule 
    public ExpectedException expectedException = ExpectedException.none(); 

    @Test 
    // Note the checked exception makes us re-throw or try/catch (we're 
    // re-throwing in this case) 
    public void calling_a_method_which_throws_a_checked_exception_which_wont_be_thrown() throws Exception { 
     throwCheckedException(false); 
    } 

    /* 
    * Put the class of the specific Exception you're looking to trigger in the 
    * annotation below. Note the test would fail if it weren't for the expected 
    * annotation. 
    */ 
    @Test(expected = Exception.class) 
    public void calling_a_method_which_throws_a_checked_exception_which_will_be_thrown_and_asserting_the_type() 
      throws Exception { 
     throwCheckedException(true); 
    } 

    /* 
    * Using ExpectedException we can also test for the message. This is my 
    * preferred method. 
    */ 
    @Test 
    public void calling_a_method_which_throws_a_checked_exception_which_will_be_thrown_and_asserting_the_type_and_message() 
      throws Exception { 
     expectedException.expect(Exception.class); 
     expectedException.expectMessage("Stack overflow example: checkedExceptionThrower"); 
     throwCheckedException(true); 
    } 

    // Note we don't need to rethrow, or try/catch as the Exception is 
    // unchecked. 
    @Test 
    public void calling_a_method_which_throws_an_unchecked_exception() { 
     expectedException.expect(Exception.class); 
     expectedException.expectMessage("Stack overflow example: uncheckedExceptionThrower"); 
     throwUncheckedException(); 
    } 

    private void throwCheckedException(boolean willThrow) throws Exception { 
     // Exception is a checked Exception 
     if (willThrow) { 
      throw new Exception("Stack overflow example: checkedExceptionThrower"); 
     } 
    } 

    private void throwUncheckedException() throws NullPointerException { 
     // NullPointerException is an unchecked Exception 
     throw new NullPointerException("Stack overflow example: uncheckedExceptionThrower"); 
    } 

} 
+0

感谢ExpectedException规则的例子。 – 2017-02-24 11:05:51

-2

您可以测试该异常与此推出:

@Test(expected = ValidationException.class) 
public void testGreaterEqual() throws ValidationException { 
    Validate.greaterEqual(new Float(-5), 0f, "error7"); 
} 
+0

你完全错过了这个问题的关键。 – 2014-11-11 12:10:21

0

一个JUnit试验是为了测试正确的行为给定的方法。被测试的方法会抛出一个错误(例如错误的参数),这是一个非常有效的方案。如果它是一个检查的异常,你必须将它添加到你的测试方法声明中,或者将它捕获到方法中,并将Assert设置为false(如果不应该发生异常)。

您可以使用@Test注释中的expected字段来告诉JUnit,如果发生异常,该测试应该通过。

@Test(expected = Exception.class) 
public void testSomething() throws Exception { 
    String expected = ""; 
    assertEquals(object.f(5), expected); 
} 

在这种情况下,被测试的方法应该抛出异常,所以测试会通过。如果从注释中删除expected = Exception.class,则发生异常时测试将失败。

相关问题