首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用junit @Rule、expectCause()和hamcrest匹配器

使用junit @Rule、expectCause()和hamcrest匹配器
EN

Stack Overflow用户
提问于 2014-03-12 16:50:45
回答 10查看 27.5K关注 0票数 27

我有个测试:

代码语言:javascript
复制
@Rule
public ExpectedException thrown = ExpectedException.none();
...
@Test
public void testMethod()
{
    final String error = "error message";
    Throwable expectedCause = new IllegalStateException(error);
    thrown.expectCause(org.hamcrest.Matchers.<Throwable>equalTo(expectedCause));
    someServiceThatTrowsException.foo();
}

当通过mvn运行测试方法时,我得到了错误:

org.junit.rules.ExpectedException.expectCause(Lorg/hamcrest/Matcher;)V:

java.lang.NoSuchMethodError

测试编译正常。

请帮帮我,不明白如何测试异常的原因?

EN

回答 10

Stack Overflow用户

发布于 2014-03-12 17:03:27

这样试试:

代码语言:javascript
复制
@Rule public ExpectedException thrown = ExpectedException.none();

@Test public void testMethod() throws Throwable {
    final String error = "error message";
    Throwable expectedCause = new IllegalStateException(error);
    thrown.expectCause(IsEqual.equalTo(expectedCause));
    throw new RuntimeException(expectedCause);
}

如有必要,请考虑不要通过等于而是通过IsInstanceOf和/或比较异常消息来检查原因。通过相等比较原因也会检查堆栈跟踪,这可能比您想要测试/检查的更多。例如,如下所示:

代码语言:javascript
复制
@Rule public ExpectedException thrown = ExpectedException.none();

@Test public void testMethod() throws Throwable {
    final String error = "error message";
    thrown.expectCause(IsInstanceOf.<Throwable>instanceOf(IllegalStateException.class));
    thrown.expectMessage(error);
    throw new RuntimeException(new IllegalStateException(error));
}
票数 28
EN

Stack Overflow用户

发布于 2016-08-30 15:49:05

简单介绍一下静态导入,同时检查类和原因异常的消息:

代码语言:javascript
复制
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

@Test
public void testThatThrowsNiceExceptionWithCauseAndMessages(){

     expectedException.expect(RuntimeException.class );
     expectedException.expectMessage("Exception message");                                           
     expectedException.expectCause(allOf(instanceOf(IllegalStateException.class),
                                        hasProperty("message", is("Cause message"))) );

     throw new RuntimeException("Exception message", new IllegalStateException("Cause message"));
}

您甚至可以使用hasProperty匹配器来断言嵌套的原因或测试"getLocalizedMessage“方法。

票数 19
EN

Stack Overflow用户

发布于 2014-09-19 19:12:54

您可以使用此处描述的自定义匹配器(http://www.javacodegeeks.com/2014/03/junit-expectedexception-rule-beyond-basics.html)来测试异常的原因。

自定义匹配器

代码语言:javascript
复制
private static class CauseMatcher extends TypeSafeMatcher<Throwable> {

    private final Class<? extends Throwable> type;
    private final String expectedMessage;

    public CauseMatcher(Class<? extends Throwable> type, String expectedMessage) {
        this.type = type;
        this.expectedMessage = expectedMessage;
    }

    @Override
    protected boolean matchesSafely(Throwable item) {
        return item.getClass().isAssignableFrom(type)
                && item.getMessage().contains(expectedMessage);
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("expects type ")
                .appendValue(type)
                .appendText(" and a message ")
                .appendValue(expectedMessage);
    }
}

测试用例

代码语言:javascript
复制
@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void verifiesCauseTypeAndAMessage() {
    thrown.expect(RuntimeException.class);
    thrown.expectCause(new CauseMatcher(IllegalStateException.class, "Illegal state"));

    throw new RuntimeException("Runtime exception occurred",
            new IllegalStateException("Illegal state"));
}
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22346275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档