[英]How to test a local variable in JUnit to enhance code coverage
I am testing a method using JUnit
API and I want to test the value of a local variable so that I get a better branch coverage. 我正在测试使用JUnit
API的方法,并且我想测试局部变量的值,以便获得更好的分支覆盖率。 In the method under test widgets
is a variable of type List
and while debugging I found out that it is not null. 在被测方法中, widgets
是类型为List
的变量,在调试时我发现它不为null。 I want to test it for Null
but I am not sure how to change it's value? 我想测试它是否为Null
但不确定如何更改它的值?
Method under test: 被测方法:
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 case for Positive Scenario 积极方案的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() );
}
Thanks in Advance 提前致谢
You need to define in your AppContext for the unit tests, your testing bean: 您需要在AppContext中为单元测试定义测试bean:
IMonitoringWidgetAggregator aggregator = (IMonitoringWidgetAggregator)AppContext.getBean( ISO + AGGREGATOR_HEALTH_PREFIX );
You do have access to the aggregator, which mean that for example if you use a CDI/IoC compatible framework like Spring (or just plain old J2EE or even something like KumulzuEE), you can use the @ContextConfiguration and @Configuration annotations to specify where do you want your @Beans to be injected from. 您确实有权访问聚合器,这意味着,例如,如果您使用像Spring这样的CDI / IoC兼容框架(或者只是普通的旧J2EE甚至是KumulzuEE之类的东西),则可以使用@ContextConfiguration和@Configuration批注指定位置您是否要从中注入@Beans。 Therefore, you can choose to inject with a mock IMonitoringWidgetAggregator or a real instance of an implementation if you want the the whole integration to be tested. 因此,如果要测试整个集成,则可以选择注入模拟IMonitoringWidgetAggregator或实现的真实实例。
A straightforward option for making the widget processing the focus of unit testing is to refactor that handling into a separate method: 使小部件处理成为单元测试重点的一个直接选择是将处理重构为单独的方法:
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;
}
With this structure, you may now easily write new JUnit test cases that focus on the widget processing performed within the preProcessWidgets
method, such as: passing a null
List
, passing an empty List
, and you are also free to mock the widgets
parameter in whatever additional way that makes sense for your application. 通过这种结构,您现在可以轻松地编写新的JUnit测试用例,这些用例着重于在preProcessWidgets
方法内执行的小部件处理,例如:传递null
List
,传递空List
,并且您还可以随意在任何条件下模拟widgets
参数。对您的应用有意义的其他方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.