简体   繁体   English

在JUnit测试(Netbeans)中显示(意外)异常消息

[英]Showing (unexpected) exception messages in JUnit tests (Netbeans)

When a JUnit test receives an unexpected RuntimeException, the NetBeans Test Results tab shows the name of the test that failed, followed by a stack trace. 当JUnit测试收到意外的 RuntimeException时,“ NetBeans测试结果”选项卡将显示失败的测试的名称,然后显示堆栈跟踪。

Ex. 防爆。

someFailedTest caused an ERROR: at (stack trace lines start here) someFailedTest导致错误:at(堆栈跟踪行从此处开始)

Not as helpful as it could be. 可能没有帮助。

While the stack trace does let us track down where a problem occurred, it would be nice if NB also showed any message that accompanied the exception. 尽管堆栈跟踪确实可以让我们跟踪出现问题的位置,但是如果NB还显示任何伴随异常的消息,那将是很好的。 (Ex. "x must be > 0 but was -2", or whatever.) (例如,“ x必须> 0但为-2”,或其他。)

As far as I can tell it doesn't seem to be making the details available. 据我所知,它似乎并没有提供细节。 Is there a way to show exception details that I'm missing? 有没有办法显示我缺少的异常详细信息?

I could use a try/catch or an error log, but is there a better way? 我可以使用try / catch或错误日志,但是有更好的方法吗?

Here's an example of a (fake) "test" which shows a message clearly in Eclipse, but not in NB. 这是一个(伪)“测试”的示例,该示例在Eclipse中清楚地显示了一条消息,但在NB中却没有。 Any insight welcome... yes I know this isn't an exception but it's the same type of problem. 欢迎任何有识之士...是的,我知道这也不例外,但这是相同类型的问题。

    @Test
    public void testTest() {

        String file = "test";
        File f = new File("../" + file);
        if (!f.exists()) {
            Assert.fail("File was not found at " + file);
        }
    }

Edit... Yep, little bit confused now - I've seen it work correctly at least once or twice, but I'm unable to reproduce it now. 编辑...是的,现在有点困惑-我已经看到它至少可以正常工作一到两次,但是我现在无法复制它。 I'll have to see if I can reproduce somewhere I can post screenshots from later... 我必须查看是否可以在某个地方复制,然后再发布屏幕截图...

Edit #2... Found some stranger behavior. 编辑#2 ...发现一些陌生人的行为。 If I do this (which is going to be my workaround for today), the test result list still looks the same, but I DO get a nice red and very helpful error message in the console: 如果我这样做(今天将作为我的解决方法),则测试结果列表看起来仍然相同,但是在控制台中我确实得到了一个很好的红色且非常有用的错误消息:

String msg = "FXML file was not found at " + file;
System.err.print("test");
Assert.fail(msg);

But if I comment out the System.err line entirely, I get nothing in the console. 但是,如果我完全注释掉System.err行,则控制台中什么也没有。 Even more bizarre, if I put an empty string, I still get nothing in the console at all (Well, maybe print() is smart enough to ignore the empty string..): 更奇怪的是,如果我输入一个空字符串,那么控制台中仍然什么也没得到(嗯,也许print()足够聪明,可以忽略该空字符串..):

String msg = "FXML file was not found at " + file;
System.err.print("");
Assert.fail(msg);

I know it's not what you want to hear, but I believe the standard way to do what you want is to use the try-catch like this: 我知道这不是您想听到的,但是我相信做您想要的事情的标准方法是使用try-catch,如下所示:

try{
    ....
}catch(Throwable t){
    //Add some code to print the details of the Throwable here
    fail(t.getMessage());
}

I'm not aware of any settings that would allow you to tell Netbeans/JUnit to display the message of an uncaught exception/throwable. 我不知道任何允许您告诉Netbeans / JUnit显示未捕获的异常/可抛出消息的设置。

My Netbeans shows the message. 我的Netbeans显示该消息。 Here's an example: 这是一个例子:

带消息的junit示例

Maybe Netbeans doesn't show the message under some circumstances. 也许Netbeans在某些情况下不显示该消息。 Maybe your top-level exception doesn't have a message associated with it. 也许您的顶级异常没有与之关联的消息。 I'm using Netbeans 8.1, and I've made no relevant configuration changes. 我正在使用Netbeans 8.1,并且没有进行任何相关的配置更改。

Here's the example you posted. 这是您发布的示例。 The message is also visible here. 该消息也在此处可见。

第二个带有消息的junit示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM