title: Testing a local variable with JUnit and Mockito
body: <p>I am writing a test case using <code>JUnit</code> and I am also using <code>Mockito</code> API wherever possible. <code>Collection&lt;AppConfigTO&gt; filteredCollection = CollectionUtils.select(configs, beanPredicate);</code> is the code where I am stuck on testing. I cannot mock <code>CollectionUtils</code> because I don't get <code>select()</code> while stubbing. If I don't use <code>Mockito</code> then how should I change the value of <code>filteredCollection</code> based on my need. I want it's size <code>&gt;0</code> so that I can cover the second if statement in my <code>Cobertura</code> report. </p>
  
  <p>Method under test:</p>
  
  <pre><code>public List&lt;AppConfigTO&gt; getAppConfig( String configCode ) {
      List&lt;AppConfigTO&gt; list = null;
  
      List&lt;AppConfigTO&gt; configs = getAppConfig();
  
      if( configs != null ) {
          EqualPredicate nameEqlPredicate = new EqualPredicate( configCode );
          BeanPredicate beanPredicate = new BeanPredicate("configCode", nameEqlPredicate);
          Collection&lt;AppConfigTO&gt; filteredCollection = CollectionUtils.select(configs, beanPredicate);
  
          if( filteredCollection != null &amp;&amp; filteredCollection.size()&gt;0 ) {
              list = new ArrayList&lt;AppConfigTO&gt;();
              for( AppConfigTO to : filteredCollection ) {
                  list.add( to );
              }
          }
      }else{
          list = appConfigDao.getAppConfig(configCode);
      }
  
      return list;
  }
  </code></pre>
  
  <p>JUnit test case:</p>
  
  <pre><code>@Test
     public void testGetAppConfig() throws Exception {
      AppConfigManager appManager = new AppConfigManager();
      cacheProvider = new TestAppConfigManager();
  
      IAppConfigManager configManager = Mockito.mock(IAppConfigManager.class);
      IAppConfigDao configDao = Mockito.mock(IAppConfigDao.class);
  
      List&lt;AppConfigTO&gt; list = new ArrayList&lt;&gt;();
  
      Mockito.when(configManager.getAppConfig()).thenReturn(list);
      Mockito.when(configDao.getAppConfig()).thenReturn(list);
  
      appManager.setAppConfigDao(configDao);
      list = appManager.getAppConfig(CONFIG_CODE);
  
      assertNull(list);
      assertNotNull(appManager.getAppConfigDao());
      assertSame("04852",this.CONFIG_CODE);
      assertNotNull(configManager.getAppConfig());
  
      // No ConfigManager
      appManager = new AppConfigManager();
      cacheProvider = new TestAppConfigManager();
  
      configManager = Mockito.mock(IAppConfigManager.class);
      configDao = Mockito.mock(IAppConfigDao.class);
  
      list = new ArrayList&lt;&gt;();
  
      Mockito.when(configManager.getAppConfig()).thenReturn(null);
      Mockito.when(configDao.getAppConfig()).thenReturn(null);
  
      appManager.setAppConfigDao(configDao);
      list = appManager.getAppConfig(CONFIG_CODE);    
  
      assertTrue(list.isEmpty());
      assertNotNull(appManager.getAppConfigDao());
      assertSame("04852",this.CONFIG_CODE);
      assertNull(configManager.getAppConfig());
  }
  </code></pre>
  
  <p>Is there any way to do it so that I can proceed with testing.</p>
  
  <p>Thanks in advance</p>