繁体   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