Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit right way of test expected exceptions

Hello guys I was wondering if this way of testing my exception is ok, i have this exception i need to throw in the second test annotation, im receiving as result a red evil bar, and a succeed and a failure, as you can guess the failure is my concern, i have a fail(); there but the reason is because i read thats the way to test the exception and now im confused.

Also i have to say im willin get the green bar because im expecting the exception, but i dont know if failure is the right way to see the answer of the expected exception.

Also if you had any advice, I would appreciate it

@Before
    public void setUp() throws Exception {
        LogPack.logPacConfig(Constants.LOGGING_FILE);
        gtfri = "+RESP:GTFRI,380502,869606020101881,INCOFER-gv65,,10,1,1,0.0,0,888.1,-84.194560,9.955602,20170220074514,,,,,,0.0,,,,100,210100,,,,20170220074517,40A2$";
        weirdProtocol = "+RESP:GRI,380502,869606020101881,INCOFER-gv65,,10,1,1,0.0,0,888.1,-84.194560,9.955602,20170220074514,,,,,,0.0,,,,100,210100,,,,20170220074517,40A2$";
        factory = new LocomotiveFactory();
    }
    @Test
    public void GTFRICreationTester_shouldPass() throws TramaConProtolocoloDesconocido {
        assertTrue(factory.createLocomotive(gtfri, false, new Date()) instanceof LocomotiveGTFRI);
    }

    @Test(expected = TramaConProtolocoloDesconocido.class)
    public void GTFRICreationTester_shouldFail()  {
        try {
            factory.createLocomotive(weirdProtocol, false, new Date());
            fail("Expected an TramaConProtolocoloDesconocido");
        } catch (TramaConProtolocoloDesconocido e) {
            //assertSame("exception thrown as expected", "no se conoce el protocolo dado para la creacion de este factory", e.getMessage());;
        }
    }
like image 660
Daniel Vega Avatar asked Feb 21 '17 17:02

Daniel Vega


People also ask

How does JUnit handle expected exception?

In JUnit, there are 3 ways to test the expected exceptions : @Test , optional 'expected' attribute. Try-catch and always fail() @Rule ExpectedException.

How can you use JUnit for testing a code that throws a desired exception?

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 do you write test cases for exceptions in JUnit 5?

In JUnit 5, to write the test code that is expected to throw an exception, we should use Assertions. assertThrows(). In the given test, the test code is expected to throw an exception of type ApplicationException or its subtype. Note that in JUnit 4, we needed to use @Test(expected = NullPointerException.


3 Answers

There is 3 most common ways to test expected exception:

First one is the most common way, but you can test only the type of expected exception with it. This test will fail if ExceptionType won't be thrown:

@Test(expected = ExceptionType.class)
public void testSomething(){
    sut.doSomething();
}

Also you cannot specify the failure message using this approach

The better option is to use ExpectedException JUnit @Rule. Here you can assert much more for expected exception

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

@Test
public void testSomething(){
    thrown.expect(ExceptionType.class);
    thrown.expectMessage("Error message");
    thrown.expectCause(is(new CauseOfExeption()));
    thrown.reportMissingExceptionWithMessage("Exception expected"); 
    //any other expectations
    sut.doSomething();
}

The third option will allow you to do the same as with using ExpectedException @Rule, but all the assertion should be written manually. However the advantage of this method is that you can use any custom assertion and any assertion library that you want:

@Test
public void testSomething(){
    try{
        sut.doSomething();
        fail("Expected exception");
    } catch(ExceptionType e) {
    //assert ExceptionType e
    } 
}
like image 131
Sergii Bishyr Avatar answered Oct 19 '22 01:10

Sergii Bishyr


You can use ExpectedException which can provide you more precise information about the exception expected to be thrown with the ability to verify error message, as follows:

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
public class TestClass {

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


    @Test
    public void GTFRICreationTester_shouldFail()  {
        expectedException.expect(TramaConProtolocoloDesconocido.class);
        factory.createLocomotive(weirdProtocol, false, new Date());
    }
}

To expolore more about it, you can refer to the blog written by me here - Expected Exception Rule and Mocking Static Methods – JUnit

like image 4
Arpit Aggarwal Avatar answered Oct 19 '22 01:10

Arpit Aggarwal


if your are using java 8, I would recommend to go for the AssertJ library

public void GTFRICreationTester_shouldFail()  {
    assertThatExceptionOfType(EXCEPTION_CLASS).isThrownBy(() -> { factory.createLocomotive(weirdProtocol, false, new Date()) })
                                               .withMessage("MESSAGE")
                                               .withMessageContaining("MESSAGE_CONTAINING")
                                               .withNoCause();         

    }

with that solution you can at one verify exception type, with message etc.

for more reading, take a look at: http://joel-costigliola.github.io/assertj/assertj-core-features-highlight.html#exception-assertion

like image 3
pezetem Avatar answered Oct 18 '22 23:10

pezetem