title: Ldap Unit Test Mock NamingEnumeration
body: <p>I'm having trouble trying to mock the NamingEnumeration. Also, I cannot get the coverage to go inside of the lambda expression. I also cannot get the coverage inside of the while loop. When I run a unit test through the methods, coverage only shows through the <code>ldapTemplate.search</code> and it skips what's inside the lambda expression and it goes through the return list. I tried adding the Mock annotation to the NamingEnumeration and Attribute objects. The while loop seem to think NamingEnumeration  is empty, because of the no coverage. </p>
  
  <p>The following results in 'Unnecessary stubbings detected in test class': <code>doReturn(true,false).when(enumeration).hasMore();</code> and <code>doReturn(attr).when(enumeration).next();</code></p>
  
  <p><strong>This is my Ldap method</strong></p>
  
  <pre><code>public List&lt;MyObject&gt; ldapQueryList(final String ldapSearch, final String key) {
          List&lt;MyObject&gt; list = new ArrayList&lt;&gt;();
  
          ldapTemplate.search("ou=User Accounts", "cn=" + ldapSearch), (Attributes attrs) -&gt; {
                  NamingEnumeration&lt;?&gt; enumeration = attrs.get(key).getAll();
                  list.addAll(addToList(enumeration));
                  return attrs;
          });
  
          return list;
      }
      public List&lt;MyObject&gt; addToList(NamingEnumeration&lt;?&gt; enumeration) throws NamingException {
          List&lt;MyObject&gt; list = new ArrayList&lt;&gt;();
          while (enumeration.hasMoreElements()) {
              final MyObject myObj = new MyObject();
              final String str = (String)enumeration.nextElement();
              myObj.setMyString(str);
              list.add(myObj);    
          }
          enumeration.close();
          return list;
      }
  </code></pre>
  
  <p><strong>This is the Unit Test</strong></p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class LdapQueryDaoTest {
      @Mock
      private LdapTemplate ldapTemplate;
      @InjectMocks
      private LdapDao ldapDao;
      @Mock 
      private NamingEnumeration&lt;?&gt; enumeration;
      @Mock 
      private Attribute attr;
      @Test
      public void ldapQueryList() throws DataAcesExcp, NamingException {
          List&lt;String&gt; searchResult = Collections.singletonList("search result");
          when(ldapTemplate.search( Mockito.anyString(), Mockito.anyString(), ArgumentMatchers.&lt;AttributesMapper&lt;String&gt;&gt; any())).thenReturn(searchResult);
          List&lt;EmployeeVo&gt; responseEntity = ldapDao.ldapQueryList(Const.EMPLOYEE_ID, "myLdapObj");
          Assert.assertNotNull(responseEntity);
      }
      @Test
      public void addToList() throws NamingException {
          doReturn(true,false).when(enumeration).hasMore();
          doReturn(attr).when(enumeration).next();
          Assert.assertNotNull(ldapQueryDaoImpl.addToList(enumeration));
      }
  }
  </code></pre>