Java – how to customize exception handling behavior in JUnit 3?

I want to use JUnit 3 to implement exception checking (as in JUnit 4) For example, I would like to be able to write the following tests:

public void testMyExceptionThrown() throws Exception {
    shouldThrow(MyException.class);

    doSomethingThatMightThrowMyException();
}

This should succeed if and only if myexception is thrown There is an exceptiontestcase class in JUnit, but I want something that each test * method can decide to use or not to use What is the best way to achieve this goal?

Solution

The solution is:

public void testMyExceptionThrown() throws Exception {
    try {
      doSomethingThatMightThrowMyException();
      fail("Expected Exception MyException");
    } catch(MyException e) {
      // do nothing,it's OK
    }
}

What's right for you?

Also take a look at this thread, where someone created a proxy solution for JUnit 3, which seems to be another possibility to solve the problem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>