Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] Guidelines on how to handle exception test in Jmock 2

Mikael Petterson

2008-05-13

Replies: Find Java Web Hosting

Author LoginPost Reply
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());
           }
       
       
        }

©2008 gg3721.com - Jax Systems, LLC, U.S.A.