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. In the method under test widgets
is a variable of type List
and while debugging I found out that it is not null. I want to test it for Null
but I am not sure how to change it's value?
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
@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