使用Mockito测试NULL条件

时间:2017-09-28 17:34:57

标签: java junit mockito

我是Cobertura的新用户,我正在尝试测试to报告要检查null的简单方案。我正在遍历列表使用每个循环,我不知道如何使if( to != null ) null。 这是条件to,我认为public boolean doHandleRequest(Object context) { ServiceContext ctx = AppContext.getThreadLocalServiceContext(); cacheProvider = ( ICacheProvider ) AppContext.getBean( Constants.SPRING_BEAN_CACHE_PROVIDER ); List<AppConfigTO> list = ( List<AppConfigTO> )getCacheProvider().getFromCache( CacheConstants.CONFIG_WSAUDITCONFIG, CacheConstants.TABLE_CACHE_KEY ); SOAPMessageContext soapMessageContext = (SOAPMessageContext)context; try { for( AppConfigTO to : list ) { if( to != null ) { if( WS_AUDIT_HANDLER_TRACE_BOTH.equalsIgnoreCase( to.getKeyValue1() ) ) { this.shouldUseMicroserviceClient( ctx.getSecurityUser() ); this.clientWrapper.addWsRecord( ctx, "Request: " + handleParams( ctx.getParams() ), getWsName(), toString( getDocument( soapMessageContext ) ) ); } } } } catch( Throwable t ) { BaseComponent component = new BaseComponent(); component.logException(ctx, CLASS_NAME, t); } return true; } 不能为空而不确定。

待测方法

@Test
public void testDoHandleRequest() throws Exception {
    EISWSAuditHandler handler = new EISWSAuditHandler();
    handler.setWsName( "VehicleRepairsTrackingService" );

    PowerMockito.when( AppContext.getBean( Constants.SPRING_BEAN_CMS_WS_AUDIT_CLIENT_WRAPPER ) ).thenReturn( clientWrapper );
    PowerMockito.when( AppContext.getThreadLocalServiceContext() ).thenReturn( serviceContext );
    PowerMockito.when(AppContext.getBean( Constants.SPRING_BEAN_CACHE_PROVIDER ) ).thenReturn( cacheProvider );

    AppConfigTO to = new AppConfigTO();
    to.setAppCode( "EIS:DEV1" );
    to.setConfigCode( "APP_WS_AUDIT_CONFIG" );
    to.setKeyName1( "VehicleRepairsTrackingService" );
    to.setKeyValue1( "BOTH" );

    List<AppConfigTO> list = new ArrayList<>();
    list.add( to );

    when( cacheProvider.getFromCache( CacheConstants.CONFIG_WSAUDITCONFIG, CacheConstants.TABLE_CACHE_KEY ) ).thenReturn( list );

    boolean request = false;
    try {
         request = handler.doHandleRequest( messageContext );
    } catch ( Exception ex ) {
        assertNotNull( ex );
    }
    assertFalse( request ); 
}

JUnit

to

以上测试适用于快乐场景,我无法使 case WM_NOTIFY: NMHDR* pHdr = (NMHDR*) lParam; if (pHdr->idFrom == IDC_LIST_RESULTS && pHdr->code == NM_CUSTOMDRAW) { return HandleCustomDraw((NMLVCUSTOMDRAW*)pHdr); } break; null

0 个答案:

没有答案