簡體   English   中英

如何在JUnit中測試局部變量以增強代碼覆蓋率

[英]How to test a local variable in JUnit to enhance code coverage

我正在測試使用JUnit API的方法,並且我想測試局部變量的值,以便獲得更好的分支覆蓋率。 在被測方法中, widgets是類型為List的變量,在調試時我發現它不為null。 我想測試它是否為Null但不確定如何更改它的值?

被測方法:

public boolean preProcess(ServiceContext ctx) throws ProcessorException {
    logMethodStartDebug( ctx, CLASS_NAME, "preProcess(ServiceContext, Object)" );

    try {

        if( Constants.YES.equalsIgnoreCase( EISSpringUtil.getMessage( ENABLE_SELF_HEALING_FLAG ) ) ) {          
            if( AppContext.getApplicationContext().containsBean( ISO + AGGREGATOR_HEALTH_PREFIX ) ) {
                IMonitoringWidgetAggregator aggregator = (IMonitoringWidgetAggregator)AppContext.getBean( ISO + AGGREGATOR_HEALTH_PREFIX );
                List<MonitoringWidget> widgets = aggregator.aggregate();

                if( widgets != null && widgets.size() > 0 ) {               
                    for( MonitoringWidget mw : widgets ) {
                        if( mw.getStatus().equals( MonitoringStatus.FAIL ) ) {
                            ctx.addMessage( ErrorMessageUtil.generateMessage( getMessageFactory(), ErrorCodeConstants.STATUS_6000, new Object[] { ctx.getSystemName(), mw.getMonitoringCode()} ) ); 
                            return false;
                        }
                    }
                }
            }
        }

        return true;
    } finally {
        logMethodEndDebug( ctx, CLASS_NAME, "preProcess(ServiceContext, Object)" );
    }
}

積極方案的JUnit測試用例

@Test
public void testPreProcess() throws Exception {
    AppContext.setApplicationContext( applicationContext );
    ServiceContext ctx = new ServiceContext();

    boolean b = preProcess( ctx );
    assertFalse( b );
    assertNotNull( ctx );
    assertNotNull( ctx.getMessages() );
    assertNotNull( ctx.getMessages().get( 0 ) );
    assertEquals( "code:6000", ctx.getMessages().get( 0 ) .getMessageCode() );
}

提前致謝

您需要在AppContext中為單元測試定義測試bean:

IMonitoringWidgetAggregator aggregator = (IMonitoringWidgetAggregator)AppContext.getBean( ISO + AGGREGATOR_HEALTH_PREFIX );

您確實有權訪問聚合器,這意味着,例如,如果您使用像Spring這樣的CDI / IoC兼容框架(或者只是普通的舊J2EE甚至是KumulzuEE之類的東西),則可以使用@ContextConfiguration和@Configuration批注指定位置您是否要從中注入@Beans。 因此,如果要測試整個集成,則可以選擇注入模擬IMonitoringWidgetAggregator或實現的真實實例。

使小部件處理成為單元測試重點的一個直接選擇是將處理重構為單獨的方法:

public boolean preProcess(ServiceContext ctx) throws ProcessorException {
    logMethodStartDebug( ctx, CLASS_NAME, "preProcess(ServiceContext, Object)" );

    try {
        if( Constants.YES.equalsIgnoreCase( EISSpringUtil.getMessage( ENABLE_SELF_HEALING_FLAG ) ) ) {          
            if( AppContext.getApplicationContext().containsBean( ISO + AGGREGATOR_HEALTH_PREFIX ) ) {
                IMonitoringWidgetAggregator aggregator = (IMonitoringWidgetAggregator)AppContext.getBean( ISO + AGGREGATOR_HEALTH_PREFIX );
                List<MonitoringWidget> widgets = aggregator.aggregate();
                return preProcessWidgets(widgets);
            }
        }

        return true;
    } finally {
        logMethodEndDebug( ctx, CLASS_NAME, "preProcess(ServiceContext, Object)" );
    }
}

private boolean preProcessWidgets(final List<MonitoringWidget> widgets) {
    if( widgets != null && widgets.size() > 0 ) {               
        for( MonitoringWidget mw : widgets ) {
            if( mw.getStatus().equals( MonitoringStatus.FAIL ) ) {
                ctx.addMessage( ErrorMessageUtil.generateMessage( getMessageFactory(), ErrorCodeConstants.STATUS_6000, new Object[] { ctx.getSystemName(), mw.getMonitoringCode()} ) ); 
                return false;
            }
        }
    }
    return true;
}

通過這種結構,您現在可以輕松地編寫新的JUnit測試用例,這些用例着重於在preProcessWidgets方法內執行的小部件處理,例如:傳遞null List ,傳遞空List ,並且您還可以隨意在任何條件下模擬widgets參數。對您的應用有意義的其他方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM