I am writing a test case using JUnit to catch a custom exception CoreServiceException that extends RuntimeException but some how I am unable to catch it. Please do not mark this duplicate because I am aware of the @test(expected=Exception.class).
Method under test:
public AgencyResponse getAgency(@QueryParam(ServiceConstants.AGENCY_CODE) String agencyCode, @QueryParam(ServiceConstants.EFFECTIVE_DATE) long effectiveDate) throws WebServiceException { ServiceContext ctx = getServiceContext(); getBaseComponent().logMethodStartDebug(ctx, CLASS_NAME, "getAgency()" ); AgencyResponse response = new AgencyResponse(); try { if( effectiveDate < 1 ) { effectiveDate = new Date().getTime(); } ctx.addParameter( ServiceConstants.AGENCY_CODE, agencyCode ); ctx.addParameter( ServiceConstants.EFFECTIVE_DATE, DateUtil.convertToDate( effectiveDate, false ) ); validateRequest( ctx, response ); IProviderChain providerChain = (IProviderChain)AppContext.getBean( ServiceConstants.AGENCY_PROVIDER_CHAIN ); response = (AgencyResponse)providerChain.retrieveData( ctx ); if( response == null ) { //precaution response = new AgencyResponse(); } if( response.getAgency() == null ) { if(ctx.getMessages()==null || ctx.getMessages().isEmpty()){ ctx.addMessage( ErrorMessageUtil.generateMessage( getBaseComponent().getMessageFactory(), ErrorCodeConstants.STATUS_2000 ) ); } } return (AgencyResponse)finalizeResponse( response, ctx ); } catch( CoreServiceException ce ) { getBaseComponent().logException(ctx, CLASS_NAME, ce); response.addMessage( ErrorMessageUtil.generateMessage( getBaseComponent().getMessageFactory(), ErrorCodeConstants.STATUS_5000 ) ); throw new WebServiceException( Response.status( Response.Status.INTERNAL_SERVER_ERROR ).entity( finalizeResponse(response, ctx) ).build() ); } catch( WebServiceException e ) { throw e; } catch( Throwable t ) { getBaseComponent().logException(ctx, CLASS_NAME, t); response.addMessage( ErrorMessageUtil.generateMessage( getBaseComponent().getMessageFactory(), ErrorCodeConstants.STATUS_5000 ) ); throw new WebServiceException( Response.status( Response.Status.INTERNAL_SERVER_ERROR ).entity( finalizeResponse(response, ctx) ).build() ); } finally { getBaseComponent().logMethodEndDebug(ctx, CLASS_NAME, "getAgency()" ); } } JUnit test:
@Test public void testgetAgency() { setUp( "Producer/Retrieve"); AppContext.setApplicationContext( applicationContext ); setSecurityUser(); AgencyResponse resp = null; try { resp = getAgency( null, 0 ); fail(); } catch( WebServiceException e ) { assertNotNull( e.getResponse() ); AgencyResponse ar = (AgencyResponse)e.getResponse().getEntity(); assertTrue( ar.getMessages().size() == 1 ); assertEquals( "message:agencyCode", ar.getMessages().get( 0 ).getMessageText() ); } resp = getAgency( "123456", 0 ); assertNotNull( resp ); assertNotNull( resp.getAgency() ); assertEquals( "123456", resp.getAgency().getAgencyCode() ); //Test Provider Exception resp = getAgency( "000000", 0 ); assertTrue( resp.getMessages().size() == 2 ); assertEquals( "message:TestProducerProvider", resp.getMessages().get( 0 ).getMessageText() ); assertEquals( "message:0", resp.getMessages().get( 1 ).getMessageText() ); //Test Provider Exception try { resp = getAgency( "999999", 0 ); fail(); } catch( WebServiceException e ) { assertNotNull( e.getResponse() ); AgencyResponse ar = (AgencyResponse)e.getResponse().getEntity(); assertTrue( ar.getMessages().size() == 2 ); assertEquals( "message:5000", ar.getMessages().get( 0 ).getMessageText() ); assertEquals( "message:0", ar.getMessages().get( 1 ).getMessageText() ); }} Any help would be appreciated. Thanks