Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to handle exceptions in junit

I wrote some test cases to test some method. But some methods throw an exception. Am I doing it correctly?

private void testNumber(String word, int number) {     try {         assertEquals(word,  service.convert(number));     } catch (OutOfRangeNumberException e) {         Assert.fail("Test failed : " + e.getMessage());     } }  @Test public final void testZero() {     testNumber("zero", 0); } 

If I pass -45, it will fail with OutOfRangeException but I am not able to test specific exception like @Test(Expected...)

like image 922
Sammy Pawar Avatar asked May 16 '13 20:05

Sammy Pawar


People also ask

How do I use exceptions in JUnit?

JUnit provides an option of tracing the exception handling of code. You can test whether the code throws a desired exception or not. The expected parameter is used along with @Test annotation. Let us see @Test(expected) in action.

How does JUnit handle checked exceptions?

The alternative would be to declare it to be thrown in your tests instead. This has the advantage of allowing you to keep these exceptions as checked and ensuring that the tests won't complain about you not handling the potential exception from being uncaught or unthrown.

How do you handle throw a new exception in JUnit test?

You can use the expected field in the @Test annotation, to tell JUnit that this test should pass if the exception occurs. In this case, the tested method should throw an exception, so the test will pass. If you remove the expected = Exception. class from the annotation, the test will fail if an exception occurs.

How do you handle and verify exceptions in junit5?

To verify the fields of an exception you'd have to add a try/catch within the test case, and within the catch block perform the additional assertions and then throw the caught exception. When using ExpectedException you have to initially declare it with ​ none() , no exception expected, which is a bit confusing.


1 Answers

An unexpected exception is a test failure, so you neither need nor want to catch one.

@Test public void canConvertStringsToDecimals() {     String str = "1.234";     Assert.assertEquals(1.234, service.convert(str), 1.0e-4); } 

Until service does not throw an IllegalArgumentException because str has a decimal point in it, that will be a simple test failure.

An expected exception should be handled by the optional expected argument of @Test.

@Test(expected=NullPointerException.class) public void cannotConvertNulls() {     service.convert(null); } 

If the programmer was lazy and threw Exception, or if he had service return 0.0, the test will fail. Only an NPE will succeed. Note that subclasses of the expected exception also work. That's rare for NPEs, but common with IOExceptions and SQLExceptions.

In the rare case that you want to test for a specific exception message, you use the newish ExpectedException JUnit @Rule.

@Rule public ExpectedException thrown= ExpectedException.none(); @Test public void messageIncludesErrantTemperature() {     thrown.expect(IllegalArgumentException.class);     thrown.expectMessage("-400"); // Tests that the message contains -400.     temperatureGauge.setTemperature(-400); } 

Now, unless the setTemperature throws an IAE and the message contains the temperature the user was trying to set, the test fails. This rule can be used in more sophisticated ways.


Your example can best be handled by:

private void testNumber(String word, int number)         throws OutOfRangeNumberException {     assertEquals(word,  service.convert(number)); }  @Test public final void testZero()         throws OutOfRangeNumberException {     testNumber("zero", 0); } 

You can inline testNumber; now, it does not help much. You can turn this into a parametrized test class.

like image 155
Eric Jablow Avatar answered Oct 07 '22 03:10

Eric Jablow