Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit test — analysing expected Exceptions [duplicate]

In JUnit, I'm currently using annotation to expect an exception in my tests.

Is there a way to analyse this exception? For example, I expect a CriticalServerException, but I also want to verify the content of the getMessage method.

like image 863
Farid Avatar asked Dec 20 '10 12:12

Farid


1 Answers

If you have JUnit 4.7 or above try ExpectedException

There is an example in this question, which is copied below:

@Rule public ExpectedException exception = ExpectedException.none();  @Test public void testRodneCisloRok(){     exception.expect(IllegalArgumentException.class);     exception.expectMessage("error1");     new RodneCislo("891415",dopocitej("891415")); } 
like image 197
Filippo Vitale Avatar answered Sep 21 '22 09:09

Filippo Vitale