Spring Boot集成测试:如何将内存LDAPConnection传入LDAPService
解决集成测试中注入内存LDAPConnection到LDAPService的方案
首先,咱们先调整一下LDAPService的getLDAPConnection方法逻辑,让它优先使用已注入的连接,没有的话再从配置创建——这样既能满足生产环境的正常使用,又能支持测试时的注入:
@Service public class LDAPService { @Autowired private LDAPServerConfig ldapServerConfig; private LDAPConnection ldapConnection; public LDAPConnection getLDAPConnection() { // 如果已经注入了连接(测试场景),直接返回 if (ldapConnection != null) { return ldapConnection; } // 生产场景:从配置创建连接,保留你原本的逻辑 // 示例: // ldapConnection = new LDAPConnection(ldapServerConfig.getHost(), ldapServerConfig.getPort()); return ldapConnection; } // 你已经写好的setter方法 public void setLdapConnection(LDAPConnection ldapConnection) { this.ldapConnection = ldapConnection; } // 你的业务方法 public void function() { LDAPConnection con = getLDAPConnection(); con.do_ldap_stuff(); } public void function1() { LDAPConnection con = getLDAPConnection(); con.do_ldap_stuff1(); } }
接下来是测试类的实现,咱们用Spring Boot的测试框架,在测试启动时初始化内存LDAP服务器,然后把它的连接注入到LDAPService中:
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class LDAPServiceIntegrationTest { @Autowired private LDAPService ldapService; private LdapInMem ldapInMem; @BeforeEach void setUp() throws Exception { // 初始化内存LDAP服务器 ldapInMem = new LdapInMem(); LDAPConnection memoryConnection = ldapInMem.startInMemLdapServer(); // 把内存连接注入到LDAPService ldapService.setLdapConnection(memoryConnection); } @Test void testFunction_UsesMemoryLDAP() { // 调用业务方法,此时会使用内存LDAP连接 ldapService.function(); // 添加断言逻辑,验证LDAP操作结果,比如检查内存LDAP中的数据是否符合预期 } @Test void testFunction1_UsesMemoryLDAP() { ldapService.function1(); // 验证function1的LDAP操作结果 } // 测试结束后关闭内存LDAP服务器,释放资源 @AfterEach void tearDown() { if (ldapInMem != null && ldapInMem.server != null) { ldapInMem.server.shutDown(true); } } }
进阶优化:用@TestConfiguration替代Setter注入
如果你不想用Setter,也可以通过测试专属的配置类来替换LDAPService的连接创建逻辑,更符合Spring的依赖注入风格:
- 先改造
LDAPService,支持构造注入:
@Service public class LDAPService { private final LDAPConnection ldapConnection; // 生产环境构造方法:用配置创建连接 @Autowired public LDAPService(LDAPServerConfig ldapServerConfig) { this.ldapConnection = createConnectionFromConfig(ldapServerConfig); } // 测试专用构造方法:直接传入内存连接 public LDAPService(LDAPConnection ldapConnection) { this.ldapConnection = ldapConnection; } private LDAPConnection createConnectionFromConfig(LDAPServerConfig config) { // 原本的连接创建逻辑 return new LDAPConnection(config.getHost(), config.getPort()); } // 业务方法... }
- 在测试类中添加
@TestConfiguration,创建内存LDAP连接并注入LDAPService:
@SpringBootTest public class LDAPServiceIntegrationTest { @Autowired private LDAPService ldapService; private static InMemoryDirectoryServer server; @TestConfiguration static class TestConfig { @Bean public LDAPConnection memoryLdapConnection() throws Exception { LdapInMem ldapInMem = new LdapInMem(); LDAPConnection connection = ldapInMem.startInMemLdapServer(); server = ldapInMem.server; return connection; } @Bean public LDAPService ldapService(LDAPConnection memoryLdapConnection) { return new LDAPService(memoryLdapConnection); } } @AfterEach void tearDown() { if (server != null) { server.shutDown(true); } } // 测试方法... }
不管用哪种方式,核心思路都是让LDAPService支持外部注入连接,测试时替换成内存LDAP的连接,生产环境则使用配置创建的连接,这样就能保证集成测试中所有LDAP操作都走内存实例啦。
内容的提问来源于stack exchange,提问作者Shubham Khandare
相关产品推荐
相关产品推荐

