Junit测试用例例外

时间:2014-12-03 08:38:46

标签: java exception junit

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如何在这里传递一些异常的对象。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

尝试一些简单的事情:

 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
 }
}