gpt4 book ai didi

java - Junit Test 用于在 LDAP 连接测试中测试 NamingEnumeration 中的数据

转载 作者:行者123 更新时间:2023-12-04 14:27:49 24 4
gpt4 key购买 nike

我编写了以下代码以从 LDAP 获取结果。从结果中,我需要提取一个条目并对其执行操作。这段代码运行良好。但问题是当我试图为它编写单元测试时。我需要加载测试数据或以某种方式模拟一些东西来为这段代码编写测试。谁能帮我指导写作方向。

讨论代码如下:

NamingEnumeration<SearchResult> results = dirContext.search(ldapSearchBase, searchFilter, searchControls);

if (results.hasMoreElements()) {

List<String> securityGroups = new ArrayList<String>();
SearchResult searchResult = results.nextElement();
NamingEnumeration ldapAttributes = searchResult.getAttributes().getAll();

while (ldapAttributes.hasMore()) {
Attribute attr = (Attribute) (ldapAttributes.next());
for (int i = 0; i < attr.size(); i++) {
if (attr.get(i).toString().startsWith("CN=GG-PaaS-logging-service")) {
String commonName = attr.get(i).toString();
int startIndex = commonName.indexOf("=") + 1;
int endIndex = commonName.indexOf(",");
commonName = commonName.substring(startIndex, endIndex);
securityGroups.add(commonName);
}
}
}
}

最佳答案

我看到(至少)7 个依赖项需要在您的代码中被模拟。

我们之所以要 mock 那么多,是因为您的代码没有遵循Demeter 法则,也就是不要与陌生人交谈。如果方法的输入直接是 ldapAttributes,则可以避免一半的模拟。

我想在您的真实代码中,您实际上是在用 securityGroups 做一些事情。在您的示例中,在离开 if...

时收集垃圾

下面是我将如何编写测试:

class LdapLoggingServiceExctractorTest{
@Rule
public MockitoRule mockito = MockitoJunit.rule()
@Mock
private DirContext dirContext;
@Mock
NamingEnumeration<SearchResult> results;
@Mock
private SearchResult loggerSearchResult;
@Mock
private Attributes ldapAttributes; // same name but different object!!
@Mock
private NamingEnumeration ldapAttributeEnum;
@Mock
private Attribute attr;

@Before
public void setup(){
doReturn(results).when(dirContext).search(any(Name.class),anyString(),any(SearchControls));
doReturn(loggerSearchResult).when(results).nextElement();
doReturn(ldapAttributes).when(loggerSearchResult).getAttributes();
doReturn(ldapAttributeEnum).when(loggerSearchResult).getAll();
doReturn(true).when(ldapAttributeEnum).hasMore();
doReturn(attr).when(ldapAttributeEnum).next();
doReturn(1).when(attr).size();
doReturn(THE_VALID_LOGGER_ENTRY_STRING).when(attr).get(0);
}


@Test
public void extractLogger_singleResultSingleAttribut_addsLoggersCommonNameToList(){
LdapLoggingServiceExctractor ldapLoggingServiceExctractor = new LdapLoggingServiceExctractor();

ldapLoggingServiceExctractor.extractLogger(dirContext);

assertThat(ldapLoggingServiceExctractor.getSecurityGroups().get(0),equalTo(LOGGER_COMMON_NAME));
}
}

Can you explain "You could avoid half of the mocking if the input to your method would be ldapAttributes directly." I didn't quite understand it. – Nirmalya Guha Khasnobis

您截取的代码开始于:

if (results.hasMoreElements()) {
List<String> securityGroups = new ArrayList<String>();
SearchResult searchResult = results.nextElement();
NamingEnumeration ldapAttributes = searchResult.getAttributes().getAll();

由于 LDAP 框架存储信息的方式,这是需要的“样板”代码。根据我对关注点分离原则的理解,ldapAttributes 的提取是不同单位的职责。所以我的方法是使用如下方法创建一个单独的类 LdapLoggingServiceExctractor:

public void addLoggerToSecurityGroup(NamingEnumeration ldapAttributes,List<String> securityGroups){
while (ldapAttributes.hasMore()) {
Attribute attr = (Attribute) (ldapAttributes.next());
for (int i = 0; i < attr.size(); i++) {
if (attr.get(i).toString().startsWith("CN=GG-PaaS-logging-service")) {
String commonName = attr.get(i).toString();
int startIndex = commonName.indexOf("=") + 1;
int endIndex = commonName.indexOf(",");
commonName = commonName.substring(startIndex, endIndex);
securityGroups.add(commonName);
}
}
}
}

然后测试将简化为:

class LdapLoggingServiceExctractorTest{
@Rule
public MockitoRule mockito = MockitoJunit.rule();
@Mock
private NamingEnumeration ldapAttributeEnum;
@Mock
private Attribute attr;
@Before
public void setup(){
doReturn(true,false).when(ldapAttributeEnum).hasMore(); // corrected to avoid endless loop...
doReturn(attr).when(ldapAttributeEnum).next();
doReturn(1).when(attr).size();
doReturn(THE_VALID_LOGGER_ENTRY_STRING).when(attr).get(0);
}
@Test
public void extractLogger_singleResultSingleAttribut_addsLoggersCommonNameToList(){
List<String> securityGroups = new ArrayList<String>();
LdapLoggingServiceExctractor ldapLoggingServiceExctractor = new LdapLoggingServiceExctractor();

ldapLoggingServiceExctractor.addLoggerToSecurityGroup(ldapAttributeEnum, securityGroups);

assertThat(securityGroups.get(0),equalTo(LOGGER_COMMON_NAME));
}
}

原始版本中要模拟的 7 个依赖项只剩下 2 个。

关闭因为这并不意味着不需要(大部分)其他模拟。
他们将被转移到新类的独立测试中,“样板”代码将被转移到该新类中。

关于java - Junit Test 用于在 LDAP 连接测试中测试 NamingEnumeration 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41894162/

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