Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Ldap单元测试模拟NamingEnumeration_Java_Spring_Junit_Mockito_Spring Ldap - Fatal编程技术网

Java Ldap单元测试模拟NamingEnumeration

Java Ldap单元测试模拟NamingEnumeration,java,spring,junit,mockito,spring-ldap,Java,Spring,Junit,Mockito,Spring Ldap,我很难模仿NamingEnumeration。此外,我无法获得lambda表达式内部的覆盖率。我也无法在while循环中获得覆盖。当我通过这些方法运行单元测试时,覆盖率仅通过ldapTemplate.search显示,它跳过lambda表达式中的内容,并通过返回列表显示。我尝试将模拟注释添加到NamingEnumeration和属性对象。while循环似乎认为namingumeration是空的,因为并没有覆盖 以下结果导致“在测试类中检测到不必要的存根”:doReturn(true,false

我很难模仿NamingEnumeration。此外,我无法获得lambda表达式内部的覆盖率。我也无法在while循环中获得覆盖。当我通过这些方法运行单元测试时,覆盖率仅通过
ldapTemplate.search
显示,它跳过lambda表达式中的内容,并通过返回列表显示。我尝试将模拟注释添加到NamingEnumeration和属性对象。while循环似乎认为namingumeration是空的,因为并没有覆盖

以下结果导致“在测试类中检测到不必要的存根”:
doReturn(true,false)
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;
    }
公共列表ldapQueryList(最终字符串ldapSearch,最终字符串键){
列表=新的ArrayList();
ldapTemplate.search(“ou=用户帐户”,“cn=”+ldapSearch),(属性属性)->{
NamingEnumeration枚举=attrs.get(key.getAll();
addAll(addToList(枚举));
返回属性;
});
退货清单;
}
公共列表addToList(NamingEnumeration枚举)引发NamingException{
列表=新的ArrayList();
while(枚举.hasMoreElements()){
最终MyObject MyObject=新的MyObject();
final String str=(String)enumeration.nextElement();
myObj.setMyString(str);
添加列表(myObj);
}
枚举。close();
退货清单;
}
这是单元测试

@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));
    }
}
@RunWith(MockitoJUnitRunner.class)
公共类LdapQueryDaoTest{
@嘲弄
私有LdapTemplate LdapTemplate;
@注射模拟
私有LdapDao LdapDao;
@嘲弄
私有NamingEnumeration枚举;
@嘲弄
私有属性属性属性;
@试验
public void ldapQueryList()抛出DataAceSECP,NamingException{
List searchResult=Collections.singletonList(“搜索结果”);
当(ldapTemplate.search(Mockito.anyString()、Mockito.anyString()、ArgumentMatchers.any())。然后返回(searchResult);
List responseEntity=ldapDao.ldapQueryList(Const.EMPLOYEE_ID,“myLdapObj”);
Assert.assertNotNull(responseEntity);
}
@试验
public void addToList()引发NamingException{
doReturn(true,false).when(枚举).hasMore();
doReturn(attr).when(枚举).next();
Assert.assertNotNull(ldapQueryDaoImpl.addToList(枚举));
}
}
我很难模仿NamingEnumeration

考虑改用实枚举。基本上,您应该只模拟那些自己创建起来非常复杂的对象(列表、迭代器和枚举就是不复杂的例子)

此外,我无法获得lambda表达式内部的覆盖率

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

while循环似乎认为namingumeration是空的,因为并没有覆盖

以下结果导致“在测试类中检测到不必要的存根”: doReturn(true,false).when(枚举).hasMore();和 doReturn(attr).when(枚举).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;
    }
}
类MyMatcher实现AttributesMapper{
列表=新的ArrayList();
公共属性mapFromAttributes(属性属性属性){
NamingEnumeration枚举=attrs.get(key.getAll();
addAll(addToList(枚举));
返回属性;
}
}
公共无效测试(){

NamingEnumeration@second感谢您的回复。我修复了我看到的不同错误
java.lang.ClassCastException:codegen.javax.naming.directory.Attributes$MockitoMock$1194996702无法转换为java.lang.String
我想我已经修复了它,它应该返回一个您正在引用的字符串
doReturn(attr)。何时(枚举).next();
是的,这是错误的类型,我错过了;)谢谢你的帮助。我明白你的意思。我现在只需要Lambda表达式中的覆盖范围。我删除了ldapTemplate.search上的when,而是尝试给
namingumeration enumeration=attrs.get(key).getAll()一个模拟值;
但我在when上找不到正确的thenReturn。我尝试了以下方法:`when(attr.get(Mockito.anyString()).getAll())。thenReturn(枚举);`I get`类型ongoingstubing中的方法thenReturn(namingumeration)不适用于参数(NamingEnumeration您可以将lambda表达式移动到另一个方法并单独测试它。(或者只在内部类中实现
AttributesMapper
接口并对该接口进行测试)我可能会遇到同样的问题,因为我无法将结果提供给attr.get(Mockito.anyString()).getAll()我添加了一些自由风格的代码(未经测试,可能无法编译)来测试lambda表达式。您应该明白了。实际上,该测试可以替代您的两个测试,因为这里没有任何内容可以介绍:剩下的是ldap搜索查询语法。
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
}