java 哪个更好, ExpectedException 或 @Test(expected=

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11426042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:04:21  来源:igfitidea点击:

Which is better, ExpectedException or @Test(expected=

javaexceptionjunit

提问by Priyank Doshi

I have code where I check the exceptions in jUnit. I want to know which of the following is a good jUnit practice?

我有代码可以检查 jUnit 中的异常。我想知道以下哪个是好的 jUnit 实践?

First

第一的

@Rule
public ExpectedException exception = ExpectedException.none();

@Test
public void checkNullObject() throws CustomException {
    exception.expect(CustomException.class);
    MyClass myClass= null;
    MyCustomClass.get(null);
}

Second

第二

@Test(expected=CustomException.class)
public void checkNullObject() throws CustomException {
    MyClass myClass= null;
    MyCustomClass.get(null);    
}

EDIT:Please note that CustomException is a unchecked custom exception. (Though it won't have any impact on this question).

编辑:请注意 CustomException 是未经检查的自定义异常。(虽然它不会对这个问题产生任何影响)。

回答by Matthew Farwell

It depends what you want to check in the exception. If all you're doing is checking that the exception is thrown, then using @Test(expected=...)is probably the easiest way:

这取决于您想在异常中检查什么。如果您所做的只是检查是否抛出了异常,那么使用@Test(expected=...)可能是最简单的方法:

@Test(expected=CustomException.class)
public void checkNullObject() throws CustomException {
  MyClass myClass= null;
  MyCustomClass.get(null);
}

However, the @Rule ExpectedException has a lot more options, including checking the message, from the javadoc:

但是,@Rule ExpectedException 有更多选项,包括检查来自javadoc的消息:

// These tests all pass.
public static class HasExpectedException {
    @Rule
    public ExpectedException thrown= ExpectedException.none();

    @Test
    public void throwsNothing() {
        // no exception expected, none thrown: passes.
    }

    @Test
    public void throwsNullPointerException() {
        thrown.expect(NullPointerException.class);
        throw new NullPointerException();
    }

    @Test
    public void throwsNullPointerExceptionWithMessage() {
        thrown.expect(NullPointerException.class);
        thrown.expectMessage("happened?");
        thrown.expectMessage(startsWith("What"));
        throw new NullPointerException("What happened?");
    }

    @Test
    public void throwsIllegalArgumentExceptionWithMessageAndCause() {
        NullPointerException expectedCause = new NullPointerException();
        thrown.expect(IllegalArgumentException.class);
        thrown.expectMessage("What");
        thrown.expectCause(is(expectedCause));
        throw new IllegalArgumentException("What happened?", cause);
    }
}

So you can check for the message, the original cause of the exception. For checking the message, you can use matchers, so you can check startsWith()and similar.

因此,您可以检查消息,即异常的原始原因。为了检查消息,您可以使用匹配器,因此您可以检查startsWith()和类似。

One reason to use the old style (Junit 3) throw/catch is if you have specific requirements. There aren't many of these, but it can happen:

使用旧样式 (Junit 3) throw/catch 的原因之一是您有特定要求。这些情况并不多,但可能会发生:

@Test
public void testMe() {
    try {
        Integer.parseInt("foobar");
        fail("expected Exception here");
    } catch (Exception e) {
        // OK
    }
}

回答by Viktor Nordling

The second version is definitely the standard way to do it. The old school way to do it, before Junit 4 looked like:

第二个版本绝对是标准的做法。在 Junit 4 之前,老派的做法是这样的:

try {
    codeThatShouldThrowException();
    fail("should throw exception");
} catch (ExpectedException expected) {
    //Expected
}

Sometimes you might want to revert to this way, for example if you want to assert something about the message in the exception.

有时您可能想恢复到这种方式,例如,如果您想对异常中的消息进行断言。