title: How to test a local variable in JUnit to enhance code coverage
body: <p>I am testing a method using <code>JUnit</code> 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 <code>widgets</code> is a variable of type <code>List</code> and while debugging I found out that it is not null. I want to test it for <code>Null</code> but I am not sure how to change it's value?</p>
  
  <p>Method under test:</p>
  
  <pre><code>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&lt;MonitoringWidget&gt; widgets = aggregator.aggregate();
  
                  if( widgets != null &amp;&amp; widgets.size() &gt; 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)" );
      }
  }
  </code></pre>
  
  <p>JUnit test case for Positive Scenario</p>
  
  <pre><code>@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() );
  }
  </code></pre>
  
  <p>Thanks in Advance</p>