在junit中使用@rule检查错误代码

时间:2022-06-13 16:00:32

I found @Rule annotation in jUnit for better handling of exception. Is there a way to check error code ?

我在jUnit中找到了@Rule注释,以便更好地处理异常。有没有办法检查错误代码?

Currently my code looks like (without @Rule):

目前我的代码看起来像(没有@Rule):

 @Test
    public void checkNullObject() {
    MyClass myClass= null;
    try {
        MyCustomClass.get(null); // it throws custom exception when null is passed
    } catch (CustomException e) { // error code is error.reason.null
        Assert.assertSame("error.reason.null", e.getInformationCode());
    }
    }

But with use of @Rule, I am doing following :

但是使用@Rule,我正在做以下事情:

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

        @Test
        public void checkNullObject() throws CustomException {
        exception.expect(CustomException .class);
        exception.expectMessage("Input object is null.");
        MyClass myClass= null;
        MyCustomClass.get(null);

        }

But, I want to do something like below:

但是,我想做类似下面的事情:

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

        @Test
        public void checkNullObject() throws CustomException {
        exception.expect(CustomException .class);
       //currently below line is not legal. But I need to check errorcode.
        exception.errorCode("error.reason.null");
        MyClass myClass= null;
        MyCustomClass.get(null);

        }

2 个解决方案

#1


4  

You can use a custom matcher on the rule with the expect(Matcher<?> matcher) method.

您可以使用expect(Matcher <?>匹配器)方法在规则上使用自定义匹配器。

For example:

例如:

public class ErrorCodeMatcher extends BaseMatcher<CustomException> {
  private final String expectedCode;

  public ErrorCodeMatcher(String expectedCode) {
    this.expectedCode = expectedCode;
  }

  @Override
  public boolean matches(Object item) {
    CustomException e = (CustomException)item;
    return expectedCode.equals(e.getInformationCode());
  }
}

and in the test:

并在测试中:

exception.expect(new ErrorCodeMatcher("error.reason.null"));

#2


1  

You can also see how the expect(Matcher<?> matcher) has been used within ExpectedException.java source

您还可以在ExpectedException.java源中查看期望(Matcher <?>匹配器)的使用方式

private Matcher<Throwable> hasMessage(final Matcher<String> matcher) {
     return new TypeSafeMatcher<Throwable>() {
      @Override
        public boolean matchesSafely(Throwable item) {
        return matcher.matches(item.getMessage());
        }
   };
}

    public void expectMessage(Matcher<String> matcher) {
         expect(hasMessage(matcher));
 }

#1


4  

You can use a custom matcher on the rule with the expect(Matcher<?> matcher) method.

您可以使用expect(Matcher <?>匹配器)方法在规则上使用自定义匹配器。

For example:

例如:

public class ErrorCodeMatcher extends BaseMatcher<CustomException> {
  private final String expectedCode;

  public ErrorCodeMatcher(String expectedCode) {
    this.expectedCode = expectedCode;
  }

  @Override
  public boolean matches(Object item) {
    CustomException e = (CustomException)item;
    return expectedCode.equals(e.getInformationCode());
  }
}

and in the test:

并在测试中:

exception.expect(new ErrorCodeMatcher("error.reason.null"));

#2


1  

You can also see how the expect(Matcher<?> matcher) has been used within ExpectedException.java source

您还可以在ExpectedException.java源中查看期望(Matcher <?>匹配器)的使用方式

private Matcher<Throwable> hasMessage(final Matcher<String> matcher) {
     return new TypeSafeMatcher<Throwable>() {
      @Override
        public boolean matchesSafely(Throwable item) {
        return matcher.matches(item.getMessage());
        }
   };
}

    public void expectMessage(Matcher<String> matcher) {
         expect(hasMessage(matcher));
 }