簡體   English   中英

Junit預期的異常失敗

[英]Junit expected exception fails

我正在嘗試測試一個加載方法,該方法將引發StreamCorruptedException。 但是當我用junit 4測試它時,測試失敗。

我的加載方法:

 public BookDataProvider(String filename) throws StreamCorruptedException { ... }

我的測試課:

public class TestClass {
   @Test (expected=StreamCorruptedException.class)
   public void wrongFileTest() throws StreamCorruptedException  {
       BookDataProvider bdp = new BookDataProvider("wrong filename");
   }
}

該方法引發異常,但測試失敗。 我做錯了什么?

從您的代碼中,我看到您的測試將錯誤的文件名傳遞給BookDataProvider構造函數,並且這不會導致StreamCorruptedException,而只會導致IOException。 由於數據反序列化失敗而發生StreamCorruptedException,這主要是由於用於寫入和讀取的流中的差異所致。 例如,如果未使用ObjectOutputStream寫入數據,則嘗試使用ObjectInputStream讀取數據時會發生這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM