Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建一个自定义的;“预期异常”;junit规则?_Java_Junit - Fatal编程技术网

Java 如何创建一个自定义的;“预期异常”;junit规则?

Java 如何创建一个自定义的;“预期异常”;junit规则?,java,junit,Java,Junit,我需要创建一个规则来检查自定义消息的异常。下面是我的尝试,但这并不完全正确,因为我只是使用标准“ExpectedException”中的方法。如何做对 public class CustomExpectedExceptionRule implements TestRule { private final ExpectedException delegate = ExpectedException.none(); public static CustomExpectedExcep

我需要创建一个规则来检查自定义消息的异常。下面是我的尝试,但这并不完全正确,因为我只是使用标准“ExpectedException”中的方法。如何做对

public class CustomExpectedExceptionRule implements TestRule {
    private final ExpectedException delegate = ExpectedException.none();

    public static CustomExpectedExceptionRule none() {
        return new CustomExpectedExceptionRule();
    }

    private CustomExpectedExceptionRule() {
    }

    public void expect(Class<? extends Throwable> type) {
        delegate.expect(type);
    }

    public void expectMessage(String message) {
        delegate.expectMessage(message);
    }

    @Override
    public Statement apply(Statement base, Description description) {
        return delegate.apply(base, description);
    }
公共类CustomExpectedExceptionRule实现TestRule{ 私有最终ExpectedException委托=ExpectedException.none(); 公共静态CustomExpectedExceptionRule none(){ 返回新的CustomExpectedExceptionRule(); } 私有CustomExpectedExceptionRule(){ }
public void expect(Class据我所知,在您的测试中,您需要:

public class MyTest {
    @Rule 
    ExpectedException expExc = ExpectedException.none();

    @Test
    public void throwsNothing() {
        // "normal tests" not affected.
    }

    @Test
    public void throwsExceptionWithSpecificTypeAndMessage() {
       expExc.expect(MyCustomException.class);
       expExc.expectMessage("substring, that passes test");// other matchers possible
       // do something that (is expected to) raise(s) 
       // MyCustomException("substring, that passes test").
    }
}
…其中,
MyCustomException.class
是一个自定义异常类(继承层次结构中可能最低的,您希望“通过”)和通过测试的
子字符串,您希望“通过”消息的(部分)

引入自定义的
TestRule
可以为您节省
1行/Test
。在这种简单的情况下,我建议您不要实现接口,而是扩展
ExternalResource
(,):

…然后像这样使用它:

public class MyTest {
    @Rule 
    CustomExpectedException expExc = new CustomExpectedException();
    ...
    @Test
    public void throwsExceptionWithSpecificTypeAndMessage() {
       expExc.myExpect("substring, that passes test");
      // do something...
    }
}

无规则方法(,):

class CustomExpectedException extends ExternalResource /*implements (!) TestRule*/ {

    private ExpectedException expExc = ExpectedException.none();

    /* Parameterize the message and also the class, if it fits your needs, 
     * alternatively && additionally implement defaults/constants/more methods.*/
    public void myExpect(String substr) {
        expExc.expect(MyCustomException.class);
        expExc.expectMessage(substr);// other matchers possible
    }
}
public class MyTest {
    @Rule 
    CustomExpectedException expExc = new CustomExpectedException();
    ...
    @Test
    public void throwsExceptionWithSpecificTypeAndMessage() {
       expExc.myExpect("substring, that passes test");
      // do something...
    }
}
public class MyTest {
    @Test
    public void throwsExceptionWithSpecificTypeAndMessage() {
        try { // !
            // do something ...
            // after that, fail the test:
            org.junit.Assert.fail("expected exception!");
        } catch (Exception exc) { // ! here i would recommend "the highest possible Exception" (in inheritance hierarchy) ...even better <code>Throwable</code>.
           // this code can be moved to a (static) util method:
           if (exc instanceof MyCustomException) {
               // make assertions on ((MyCustomException) exc).getMessage();
           } else {
               org.junit.Assert.fail("UNexpected exception!");
               // or rethrow:
               // throw exc;
           }
        }          
    }
}