gpt4 book ai didi

java - Ldap 单元测试模拟命名枚举

转载 作者:行者123 更新时间:2023-12-02 09:39:12 26 4
gpt4 key购买 nike

我在尝试模拟 NamingEnumeration 时遇到问题。另外,我无法将覆盖范围放入 lambda 表达式内部。我也无法获得 while 循环内部的覆盖范围。当我通过方法运行单元测试时,覆盖率仅通过 ldapTemplate.search 显示,并且它会跳过 lambda 表达式内的内容并遍历返回列表。我尝试将 Mock 注释添加到 NamingEnumeration 和 Attribute 对象。 while 循环似乎认为 NamingEnumeration 为空,因为没有覆盖。

以下结果导致“在测试类中检测到不必要的 stub ”:doReturn(true,false).when(enumeration).hasMore();doReturn(attr).when(枚举).next();

这是我的 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;
}

这是单元测试

@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.

考虑使用真正的枚举。基本上,您应该只模拟那些过于复杂而无法自行创建的对象(列表、迭代器和枚举是不复杂的示例)。

Also, I cannot get the coverage to go inside of the lambda expression.

它按预期工作。您模拟(读取替换)搜索方法,因此不会对 lambda 表达式进行求值,因为它已经具有定义的结果。

The while loop seem to think NamingEnumeration is empty, because of the no coverage.

The following results in 'Unnecessary stubbings detected in test class':doReturn(true,false).when(enumeration).hasMore(); anddoReturn(attr).when(enumeration).next();

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));
}
<小时/>

您可以单独验证 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
}

关于java - Ldap 单元测试模拟命名枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57235568/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com