简体   繁体   English

Ldap单元测试模拟命名枚举

[英]Ldap Unit Test Mock NamingEnumeration

I'm having trouble trying to mock the NamingEnumeration. 我在尝试模拟NamingEnumeration时遇到麻烦。 Also, I cannot get the coverage to go inside of the lambda expression. 另外,我无法覆盖Lambda表达式。 I also cannot get the coverage inside of the while loop. 我也无法在while循环中获得覆盖率。 When I run a unit test through the methods, coverage only shows through the ldapTemplate.search and it skips what's inside the lambda expression and it goes through the return list. 当我通过这些方法运行单元测试时,覆盖率仅通过ldapTemplate.search显示,并且跳过了lambda表达式中的内容,并通过了返回列表。 I tried adding the Mock annotation to the NamingEnumeration and Attribute objects. 我尝试将Mock注释添加到NamingEnumeration和Attribute对象。 The while loop seem to think NamingEnumeration is empty, because of the no coverage. while循环似乎认为NamingEnumeration为空,因为没有覆盖。

The following results in 'Unnecessary stubbings detected in test class': doReturn(true,false).when(enumeration).hasMore(); 以下结果导致“在测试类中检测到不必要的存根”: doReturn(true,false).when(enumeration).hasMore(); and doReturn(attr).when(enumeration).next(); doReturn(attr).when(enumeration).next();

This is my Ldap method 这是我的Ldap方法

public List<MyObject> ldapQueryList(final String ldapSearch, final String key) {
        List<MyObject> list = new ArrayList<>();

        ldapTemplate.search("ou=User Accounts", "cn=" + ldapSearch), (Attributes attrs) -> {
                NamingEnumeration<?> enumeration = attrs.get(key).getAll();
                list.addAll(addToList(enumeration));
                return attrs;
        });

        return list;
    }
    public List<MyObject> addToList(NamingEnumeration<?> enumeration) throws NamingException {
        List<MyObject> list = new ArrayList<>();
        while (enumeration.hasMoreElements()) {
            final MyObject myObj = new MyObject();
            final String str = (String)enumeration.nextElement();
            myObj.setMyString(str);
            list.add(myObj);    
        }
        enumeration.close();
        return list;
    }

This is the Unit Test 这是单元测试

@RunWith(MockitoJUnitRunner.class)
public class LdapQueryDaoTest {
    @Mock
    private LdapTemplate ldapTemplate;
    @InjectMocks
    private LdapDao ldapDao;
    @Mock 
    private NamingEnumeration<?> enumeration;
    @Mock 
    private Attribute attr;
    @Test
    public void ldapQueryList() throws DataAcesExcp, NamingException {
        List<String> searchResult = Collections.singletonList("search result");
        when(ldapTemplate.search( Mockito.anyString(), Mockito.anyString(), ArgumentMatchers.<AttributesMapper<String>> any())).thenReturn(searchResult);
        List<EmployeeVo> 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));
    }
}

I'm having trouble trying to mock the NamingEnumeration. 我在尝试模拟NamingEnumeration时遇到麻烦。

Consider using a real enumeration instead. 考虑改为使用实际枚举。 Basically you should only mock objects that are to complex to create yourself (Lists, Iterators and Enumeration are examples for things that are not). 基本上,您只应模拟复杂的对象来创建自己(列表,迭代器和枚举是非对象的示例)。

Also, I cannot get the coverage to go inside of the lambda expression. 另外,我无法覆盖Lambda表达式。

It works as expected. 它按预期工作。 You mocked (read replaced) the search method, so there is no evaluation of the lambda expression as it already has a defined result. 您嘲笑(用读替换)搜索方法,因此没有lambda表达式的求值,因为它已经具有定义的结果。

The while loop seem to think NamingEnumeration is empty, because of the no coverage. while循环似乎认为NamingEnumeration为空,因为没有覆盖。

The following results in 'Unnecessary stubbings detected in test class': doReturn(true,false).when(enumeration).hasMore(); 以下结果导致“在测试类中检测到不必要的存根”:doReturn(true,false).when(enumeration).hasMore(); and doReturn(attr).when(enumeration).next(); 和doReturn(attr).when(enumeration).next();

hasMore and next are typos on your end as the methods called in your example that are called are hasMoreElements and nextElement. hasMore和next是错别字,因为在示例中调用的方法hasMoreElements和nextElement。

@Test
public void addToList() throws NamingException {
   doReturn(true,false).when(enumeration).hasMoreElements();
   doReturn(attr).when(enumeration).nextElement();
   Assert.assertNotNull(ldapQueryDaoImpl.addToList(enumeration));
}

You could verify the lambda expression seperately, example see below: 您可以单独验证lambda表达式,示例如下所示:

class MyMatcher implements AttributesMapper<Attributes> {

    List<MyObject> list = new ArrayList<>();

    public Attributes mapFromAttributes(Attributes attrs) {

        NamingEnumeration<?> enumeration = attrs.get(key).getAll();
        list.addAll(addToList(enumeration));
        return attrs;
    }
}
public void test() {

  NamingEnumeration<? extends Attribute> enumeration = ...

  Attribute attributeMock = mock(Attribute.class);
  when(attributeMock.getAll()).thenReturn(enumeration);

  Attributes attributesMock = mock(Attributes.class);
  when(attributesMock.get(any(String.class)).thenReturn(attributeMock);

  MyMatcher matcher = new MyMatcher();
  matcher.mapFromAttributes(attr);

  // assert ... matcher.list
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM