我正在尝试在我的一个 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/