2014-11-04 82 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,則發生異常時測試將失敗。

相關問題