捕捉异常

时间:2016-03-31 21:17:16

标签: java junit

我正在使用JUnit编写一个测试用例,以捕获扩展CoreServiceException的自定义异常RuntimeException,但有些我无法捕获它。请不要标记此副本,因为我知道@test(expected=Exception.class)

待测方法:

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

任何帮助将不胜感激。 感谢

0 个答案:

没有答案