如何模拟LDAP模板

时间:2017-11-30 10:06:04

标签: java junit ldap powermockito spring-ldap

我想模仿

@Mock
private LdapTemplate ldapTemplate;

但它给出了错误:

  

org.mockito.exceptions.base.MockitoException:       Mockito无法在字段上注入模拟依赖项'ldapTemplate'       'private org.springframework.ldap.core.LdapTemplate com.bt.resolve.dao.CadGroupsDAOImpl.ldapTemplate'       其类型'com.bt.resolve.dao.CadGroupsDAOImpl'在测试中由@InjectMocks注释。

我也失败了因为:null

有人可以解释一下模拟LdapTemplate的正确方法吗?

@Repository("cadGroupsDAO")
public class CadGroupsDAOImpl implements CadGroupsDAO {

    private LdapTemplate ldapTemplate;

     /**
     * Logger for trace and errors.
     */
    private static final Logger LOGGER = Logger.getLogger(CadGroupsDAOImpl.class);
    long startTime = System.currentTimeMillis();


    public void setLdapTemplate(LdapTemplate ldapTemplate) {
        LdapContextSource contextSource = (LdapContextSource) ldapTemplate.getContextSource();

        Map<String, Object> baseEnvironmentProperties = new HashMap<>();

        baseEnvironmentProperties.put("java.naming.ldap.factory.socket", BlindSSLSocketFactoryTest.class.getName());
        contextSource.setBaseEnvironmentProperties(baseEnvironmentProperties);
        contextSource.afterPropertiesSet();
        this.ldapTemplate = new LdapTemplate(contextSource);
        this.ldapTemplate.setIgnorePartialResultException(true);
    }

    @Override
    public List<LdapAttributeDTO> getCadGroups() throws UnableToFetchCadGroupsException {

        LOGGER.info("Entered into CadGroupsDAOImpl.getCadGroups()");

        List<LdapAttributeDTO> cadGroupList= new ArrayList<>();
        // set the options
        try {
            SearchControls searchControls = new SearchControls();
            searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            searchControls.setTimeLimit(Integer.parseInt(SpringUtils.getProperty("LDAP_CONNECTION_TIMEOUT"))); 
            searchControls.setCountLimit(Integer.parseInt(SpringUtils.getProperty("LDAP_CONNECTION_SEARCH_LIMIT")));
            searchControls.setReturningAttributes(new String[] { "cn" });

            LOGGER.debug("Obtained time out"+searchControls.getTimeLimit()+"and Count Limit"+searchControls.getCountLimit());

            // set the filter
            String filter = "cn=*";

            // fetch results from ldap
             cadGroupList = ldapTemplate.search("", filter, searchControls,
                    new LdapAttributeMapper());

        } catch (Exception e) {
            LOGGER.error("Exception occured in getCadGroups",e);
            throw new UnableToFetchCadGroupsException();
        }

        LOGGER.info("Exiting from CadGroupsDAOImpl.getCadGroups() with List of Cad Grops:"+cadGroupList+ "And TIME_TAKEN_TO_FINISH "+ (System.currentTimeMillis() - startTime) + "MILIS");
        return cadGroupList;
    }

}

这是我写的测试

@RunWith(MockitoJUnitRunner.class)
public class CadGroupsDAOTest {

    @InjectMocks
    CadGroupsDAOImpl CadGroupsDAO;

    @Mock
    private LdapTemplate ldapTemplate;

    @Mock
    private BTOSPropertyManager btPropertyManager;


    LdapContextSource contextSource = new LdapContextSource();

    @Before
    public void setUp() throws Exception {
        given(ldapTemplate.getContextSource()).willReturn(contextSource);
        SpringUtils.setBtPropertyManager(btPropertyManager);
        given(btPropertyManager.getProperty(anyString(), anyString())).will(SpringPropertyInstance.getSpringProperty());
    }

    @Test 
    public void getCadGroups_Success() throws UnableToFetchCadGroupsException{

        try {
            List<LdapAttributeDTO> cadGroups =  CadGroupsDAO.getCadGroups();
        } catch (Exception e) {
            // TODO Auto-generated catch blocker

        }

        //need to write for success scenario
    }
}

0 个答案:

没有答案