Guidelines on how to handle exception test in Jmock 2

View: New views
2 Messages — Rating Filter:   Alert me  

Guidelines on how to handle exception test in Jmock 2

by eraonel :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Guidelines on how to handle exception test in Jmock 2

Hi,

When accessing the createAddress I tell the mock to throw a ServletParseException but since the code under test ( see first code snippet below) after catching throw a SipScenarioExcpetion I guess that this is the exception that I shall catch in my test?

 
Also if I don't add a throws Exception in the test-method declaration the test code will complain that I am not handling the ServletParseException. Is it correct to add a throws Exception to my test-method?

Shall I use context.assertIsSatisfied() or assertNotNull("Exception was thrown"+sse.getReason()) for assert?

br,

//mikael

public void create(){

        try {
            toAddress = sipFactory.createAddress(address);
        } catch (ServletParseException e) {
            throw new SipScenarioException(Reason.PARSEERROR);
        }
............

}

This is the test using Jmock 2:

@Test
public void testCreateWithParseError() throws Exception {
        Home home = new Home();
       
      try{
       
        //Expectations
        context.checking(new Expectations() {

            {
                .....
                allowing(mockSipFactory).createAddress(SIP_ADDRESS);
                will(throwException(new ServletParseException("Could not parse address!")));
               
            }
        });
       
        home.create();
       
         }catch (SipScenarioException sse){
                        assertNotNull("Exception was thrown"+sse.getReason());
           }
       
       
        }


Re: Guidelines on how to handle exception test in Jmock 2

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Your test should look for the SipScenarioException, since that's the  
externally visible effect.

You'll also need to add the throws clause to the test method if you  
don't catch the exception to keep the compiler happy.

If you're using JUnit4, there's an annotation  
@Test(expected=SipScenarioException.class) that will fail unless that  
exception is thrown.

On 13 May 2008, at 11:13, Mikael Petterson wrote:

> Hi,
>
> When accessing the createAddress I tell the mock to throw a
> ServletParseException but since the code under test ( see first code
> snippet below) after catching throw a SipScenarioExcpetion I guess  
> that
> this is the exception that I shall catch in my test?
>
> Also if I don't add a throws Exception in the test-method declaration
> the test code will complain that I am not handling the
> ServletParseException. Is it correct to add a throws Exception to my
> test-method?
>
> Shall I use context.assertIsSatisfied() or assertNotNull("Exception  
> was
> thrown"+sse.getReason()) for assert?
>
> br,
>
> //mikael
>
> public void create(){
>
>        try {
>            toAddress = sipFactory.createAddress(address);
>        } catch (ServletParseException e) {
>            throw new SipScenarioException(Reason.PARSEERROR);
>        }
> ............
>
> }
>
> This is the test using Jmock 2:
>
> @Test
> public void testCreateWithParseError() throws Exception {
>        Home home = new Home();
>
>      try{
>
>        //Expectations
>        context.checking(new Expectations() {
>
>            {
>                .....
>                allowing(mockSipFactory).createAddress(SIP_ADDRESS);
>                will(throwException(new ServletParseException("Could  
> not
> parse address!")));
>
>            }
>        });
>
>        home.create();
>
>         }catch (SipScenarioException sse){
> assertNotNull("Exception was
> thrown"+sse.getReason());
>   }
>
>
> }

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


LightInTheBox - Buy quality products at wholesale price