0

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

7
  • So it crashes the program instead of caching it? If so please post the stacktrace Commented Mar 31, 2016 at 21:21
  • It throws AssertionFailedError where I am invoking JUnit's fail(). Commented Mar 31, 2016 at 21:26
  • If it's not being caught are you certain it's actually being thrown when you think it is? Commented Mar 31, 2016 at 21:26
  • From the code that you've posted there's no way for us to know if that exception actually gets thrown at all, and if it's not being caught then it's likely that it isn't. Commented Mar 31, 2016 at 21:27
  • stackoverflow.com/questions/19164020/… Commented Mar 31, 2016 at 21:35

0