How do you customize exception handling behavior in JUnit 3?

I want to implement exception checking (for example, in JUnit 4) using JUnit 3. For example, I would like to write such tests as follows:

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

    doSomethingThatMightThrowMyException();
}

This should succeed if and only if a MyException is thrown. There is an ExceptionTestCase class in JUnit, but I want something that each test * method may decide to use or not to use. What is the best way to achieve this?

+3
source share
3 answers

Will there be a solution:

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

suitable for what you think?

, - - JUnit3, , , .

+9

, , JUnit3 ( ): catch-exception.

+2

- Execute Around idiom, , .

More difficult is to note that TestCase- this is just Test. I forgot the details, but we can override the execution of the test (which initially refers to the run(TestResult)one specified in Test). In this override, we can put try-catch according to Execute Around. The method testXxxmust call the set method to set the expected type of exception.

+1
source

Source: https://habr.com/ru/post/1704490/


All Articles