HowTo: Testing more than one Exception Condition using @Rule
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   dleal
Posted On:   Monday, August 25, 2014 02:08 PM

Dear member, I would like to test two exception conditions on the same method, but when I try to test a second condition. The execution, never pass the rest of the lines code.  I am using @Rule, for example using the source code from the junit web page:   public void shouldTestExceptionMessage() throws IndexOutOfBoundsException {         List<Object> list = new ArrayList<Object>();           // First testing condition         thrown1 .expect(IndexOutOfBoundsException. class );         thrown1 .expectMessage( &q   More>>

Dear member,

I would like to test two exception conditions on the same method, but when I try to test a second condition. The execution, never pass the rest of the lines code.  I am using @Rule, for example using the source code from the junit web page:

  public void shouldTestExceptionMessage() throws IndexOutOfBoundsException {

        List<Object> list = new ArrayList<Object>();

 

        // First testing condition

        thrown1 .expect(IndexOutOfBoundsException. class );

        thrown1 .expectMessage( "Index: 0, Size: 0" );

        list .get(0); // execution will never get past this line

        

        // Second testing condition

        thrown1 .expect(IndexOutOfBoundsException. class );

        thrown1 .expectMessage( "Index: 0, Size: -1" );

        list .get(-1);

    }

Is there any way to make such kind of testing using @Rule?, I know using try-catch block it is possible, but you have to write so much line of codes.

Thanks in advance,

David

   <<Less
About | Sitemap | Contact