0
public class xyzException extends Exception{
private final Object mSource;
private final Object mObjectInError;
private final Throwable mCause;
public xyzException(Object obj1, Object obj2, Throwable cause, String message){
super(message);
this.mSource = obj1;
this.mObjectInError = obj2;
this.mCause = cause;
}
public static String getStackTrace(xyzException e) {
StringWriter sw = new StringWriter();
PrintWriter w = new PrintWriter(sw);
e.printStackTrace(w);
try {
w.close();
}
catch (final Exception ignore) {
}
return sw.toString();
}
我都要在JUnit写了这个测试用例的getStackTrace()方法:错误输出断言在JUnit测试案例例外
public class xyzExceptionTest {
private String message;
private String source;
private xyzException obj;
@Before
public void setUp() throws Exception {
message = "Exception";
source = "source";
obj = new xyzException(source, "MyObject.class", new NullPointerException(), message);
}
@Test
public void getStackTraceTest() {
assertEquals("GetStackTrace unexpected Value", "MyObject.class",xyzException.getStackTrace(obj));
}
}
但是,这是越来越失败。我该如何纠正它以及失败的原因? 错误是断言获取异常。 我应该如何避免这种情况。
用适当的方法编辑assertEquals .. – 2014-12-04 10:21:24
我明白了。这里的代码充满了“小错误”。例如,它不会编译,因为没有字段'mSource','mObjectInError'和'mCause'。它们似乎不重要,因为它们未被使用。他们是否应该使用? – 2014-12-04 10:27:26
问题归结于为什么您会期望'new NullPointerException()。printStackTrace()'调用返回“MyObject.class”。你想要达到什么目标? – 2014-12-04 10:28:51