本文介绍了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如何传递一些对象,例外在这里。
任何帮助将不胜感激。

I need to write Junit test case for this simple exception class having some more methods.But Since I am new to Junit, Idk how to pass some object with exception here.Any help will be appreciated.

推荐答案

尝试一些简单的例子:

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

这篇关于Junit测试用例异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 18:05