2014-12-03 84 views
-3
public class TipException extends Exception { 
    private final Object mSource; 
    private final Object mObjectInError; 
    private final Throwable mCause; 

    public TipException(Object source, Object objectInError, Throwable cause, String message) { 
     super(message); 
     this.mSource = source; 
     this.mObjectInError = objectInError; 
     this.mCause = cause; 
    } 
} 

我需要爲這個簡單的異常類編寫Junit測試用例,它有更多的方法。 但是因爲我是Junit的新手,Idk如何在這裏傳遞一些異常對象。 任何幫助將不勝感激。Junit Test case for exception

+3

異常也是類和別人一樣,所以你可以測試構造函數和其他人一樣「業務」的邏輯。你應該測試什麼和如何測試? – Smutje 2014-12-03 08:40:09

回答

2

嘗試喜歡簡單的東西:

public class TipExceptionTest extends TestCase { 
private final String message = "Exception"; 
private final TipException tipException= new TipException("source", "MyObject.class", new NullPointerExcepiton(), message); 

@Test 
public void testTipExceptionWithMessage() { 
    Assert.assertEquals(tipException.getMessage(), message);//and if you exposed getter/setter for source etc you could assert as well 
} 
}