testing - 使用 Spring LDAP 嵌入式服务器运行测试时“地址已在使用”

标签 testing spring-boot spring-ldap unboundid-ldap-sdk

我正在尝试在我的一个 Spring Boot 项目中使用 Spring LDAP,但在运行多个测试时出现“地址已在使用”错误。

我已经在本地克隆了示例项目: https://spring.io/guides/gs/authenticating-ldap/

...并且刚刚添加了通常由 Spring Boot 创建的样板测试以验证应用程序上下文是否正确加载:

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyApplicationTests {
    @Test
    public void contextLoads() {
    }
}

如果单独运行,则此测试通过。一旦 LdapAuthenticationTests 和 MyApplicationTests 一起运行,我就会收到上面针对后者的错误。

稍微调试后,我发现发生这种情况是因为系统试图生成嵌入式服务器的第二个实例。

我确定我在配置中遗漏了一些非常愚蠢的东西。 我该如何解决这个问题?

最佳答案

我有一个类似的问题,看起来你配置了一个静态端口(就像我的情况一样)。

根据 this article :

Spring Boot starts an embedded LDAP server for each application context. Logically, that means, it starts an embedded LDAP server for each test class. Practically, this is not always true since Spring Boot caches and reuses application contexts. However, you should always expect that there is more than one LDAP server running while executing your tests. For this reason, you may not declare a port for your LDAP server. In this way, it will automatically uses a free port. Otherwise, your tests will fail with “Address already in use”

因此,根本不定义 spring.ldap.embedded.port 可能是一个更好的主意。

关于testing - 使用 Spring LDAP 嵌入式服务器运行测试时“地址已在使用”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47291853/

相关文章:

javascript - Angular - 测试带有附加集的@Input

java - 如何从 ldap DirContextOperations 获取 memberOf 属性

spring - 为该 cookie 指定的域无效

angular - docker容器中的前端和后端

java - 带有错误 Active Directory Ldap 凭据的 session /Redis 序列化错误的 Spring Boot

java - 为什么 SpringLDAP/普通 Java AD 查询中的 accountExpires 和 userAccountControl 过滤器不能按预期工作?

testing - 使用 Silex 测试框架发送 post 参数

asp.net mvc nhibernate 单元测试

javascript测试

spring - @ExceptionHandler 如何从 HttpServletRequest 获取原始 json post