How can I use JUnit's ExpectedException to check the state that's only on a child Exception?

柔情痞子 提交于 2019-12-11 03:33:16

问题


I'm trying to refactor this old code that does not use ExpectedException so that it does use it:

    try {
        //...
        fail();
    } catch (UniformInterfaceException e) {
        assertEquals(404, e.getResponse().getStatus());
        assertEquals("Could not find facility for aliasScope = DOESNTEXIST", e.getResponse().getEntity(String.class));
    }

And I can't figure out how to do this because I don't know how to check the value of e.getResponse().getStatus() or e.getResponse().getEntity(String.class) in an ExpectedException. I do see that ExpectedException has an expect method that takes a hamcrest Matcher. Maybe that's the key, but I'm not exactly sure how to use it.

How do I assert that the exception is in the state I want if that state only exists on the concrete exception?


回答1:


The "best" way is a custom matcher like the ones described here: http://java.dzone.com/articles/testing-custom-exceptions

So you would want something like this:

import org.hamcrest.Description;
import org.junit.internal.matchers.TypeSafeMatcher;

public class UniformInterfaceExceptionMatcher extends TypeSafeMatcher<UniformInterfaceException> {

public static UniformInterfaceExceptionMatcher hasStatus(int status) {
    return new UniformInterfaceExceptionMatcher(status);
}

private int actualStatus, expectedStatus;

private UniformInterfaceExceptionMatcher(int expectedStatus) {
    this.expectedStatus = expectedStatus;
}

@Override
public boolean matchesSafely(final UniformInterfaceException exception) {
    actualStatus = exception.getResponse().getStatus();
    return expectedStatus == actualStatus;
}

@Override
public void describeTo(Description description) {
    description.appendValue(actualStatus)
            .appendText(" was found instead of ")
            .appendValue(expectedStatus);
}

}

then in your Test code:

@Test
public void someMethodThatThrowsCustomException() {
    expectedException.expect(UniformInterfaceException.class);
    expectedException.expect(UniformInterfaceExceptionMatcher.hasStatus(404));

    ....
}


来源:https://stackoverflow.com/questions/22491137/how-can-i-use-junits-expectedexception-to-check-the-state-thats-only-on-a-chil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!