Can junit check that the method will throw an exception?

Could you please tell me if it is common practice to write a method (example: JUnit Test) that throws an exception, for example:

class A {
    public String f(int param) throws Exception {
        if (param == 100500)
            throw new Exception();
        return "";
    }
}

private A object = new A();

@Test
public void testSomething() throws Exception {
    String expected = "";
    assertEquals(object.f(5), expected);
}

      

In fact, the method f()

will not throw an exception for this parameter (5), but nevertheless, I have to declare this exception.

+8


source to share


4 answers


Yes, this is completely normal, and if it raises an exception, the test will fail.

You need to specify what the method produces Exception

, even if you know there is no specific case (this check is done by the compiler).



In this case, you would expect to object.f(5)

return an empty string. Any other result (non-empty string or exception) will result in a failed test case.

+7


source


If the method you are calling throws a thrown exception, yes, you will need to try catch or rethrow. It's good to do it from the dough itself. There are many ways to test Exception using JUnit. I tried to give a short summary below:



import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
 * Example uses Kent Beck - Test Driven Development style test naming
 * conventions
 */
public class StackOverflowExample {

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

    @Test
    // Note the checked exception makes us re-throw or try / catch (we're
    // re-throwing in this case)
    public void calling_a_method_which_throws_a_checked_exception_which_wont_be_thrown() throws Exception {
        throwCheckedException(false);
    }

    /*
     * Put the class of the specific Exception you're looking to trigger in the
     * annotation below. Note the test would fail if it weren't for the expected
     * annotation.
     */
    @Test(expected = Exception.class)
    public void calling_a_method_which_throws_a_checked_exception_which_will_be_thrown_and_asserting_the_type()
            throws Exception {
        throwCheckedException(true);
    }

    /*
     * Using ExpectedException we can also test for the message. This is my
     * preferred method.
     */
    @Test
    public void calling_a_method_which_throws_a_checked_exception_which_will_be_thrown_and_asserting_the_type_and_message()
            throws Exception {
        expectedException.expect(Exception.class);
        expectedException.expectMessage("Qaru example: checkedExceptionThrower");
        throwCheckedException(true);
    }

    // Note we don't need to rethrow, or try / catch as the Exception is
    // unchecked.
    @Test
    public void calling_a_method_which_throws_an_unchecked_exception() {
        expectedException.expect(Exception.class);
        expectedException.expectMessage("Qaru example: uncheckedExceptionThrower");
        throwUncheckedException();
    }

    private void throwCheckedException(boolean willThrow) throws Exception {
        // Exception is a checked Exception
        if (willThrow) {
            throw new Exception("Qaru example: checkedExceptionThrower");
        }
    }

    private void throwUncheckedException() throws NullPointerException {
        // NullPointerException is an unchecked Exception
        throw new NullPointerException("Qaru example: uncheckedExceptionThrower");
    }

}

      

+3


source


A JUnit-Test is designed to test a given method for correct behavior. This is a perfectly valid scenario when the tested method throws an error (for example, due to incorrect parameters). If it is a checked exception, you need to either add it to your test method declaration or catch it in the method and Assert is false (unless the exception should be thrown).

You can use a field expected

in the annotation @Test

to tell JUnit that this test should pass if an exception is thrown.

@Test(expected = Exception.class)
public void testSomething() throws Exception {
    String expected = "";
    assertEquals(object.f(5), expected);
}

      

In this case, the method under test should throw an exception, so the test will pass. If you remove expected = Exception.class

from annotation, the test will fail if an exception is thrown.

+2


source


You can check that the exception is being thrown with this:

@Test(expected = ValidationException.class)
public void testGreaterEqual() throws ValidationException {
    Validate.greaterEqual(new Float(-5), 0f, "error7");
}

      

-2


source







All Articles