在例外的junit测试用例中断言输出错误

时间:2014-12-04 08:24:02

标签: java unit-testing exception junit exception-handling

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()方法编写了这个测试用例:

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

但这已经失败了。我该如何纠正它以及失败的原因? 断言的错误是获得异常。 我该怎样避免这种情况。

1 个答案:

答案 0 :(得分:0)

我简化了你的代码,向你展示没有什么特别的。没有功能变化,我只删除了死代码。然后代码归结为:

public class XyzException extends Exception {

    public static String getStackTrace(XyzException e) {
        StringWriter sw = new StringWriter();
        try (PrintWriter w = new PrintWriter(sw)) {
            e.printStackTrace(w);
            return sw.toString();
        } 
    }

    public XyzException(String message) {
        super(message);
    }

}

public class XyzExceptionTest {
    private String message;
    private XyzException exception;

    @Before
    public void setUp() throws Exception {
        message = "A message";
        exception = new XyzException(message);
    }

    @Test
    public void getStackTraceTest() {
        System.out.println(XyzException.getStackTrace(obj));
        assertEquals("GetStackTrace unexpected Value", "MyObject.class", XyzException.getStackTrace(exception));
    }
}

现在,您可以看到XyzException类现在什么都不是,它只是调用带有传递message的父构造函数,没有别的。

静态getStackTrace(XyzException e)方法也没什么特别的。它接受传递的异常并将其堆栈跟踪作为一个长String返回。在我的设置中,该堆栈跟踪如下所示:

cz.slanec.playground.XyzException: A message
    at cz.slanec.playground.XyzExceptionTest.setUp(XyzExceptionTest.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

为什么在地球上你会期望"MyObject.class"出现在那里的任何地方?代码可以正常工作,例如Throwable#printStackTrace()

从头开始,定义您的类和预期行为,然后编写测试以验证所述行为。现在,测试是错误的。