繁体   English   中英

在Java Spring Boot中,如何在集成测试中将内存中的LDAPConnection对象传递给ldapService?

[英]In java Spring Boot ,How to pass in-memory LDAPConnection object to ldapService in integration testing?

我正在为我的API编写集成测试。 我必须测试流程,这就是为什么要实现内存ldap的原因。 我使用了InMemoryDirectoryServer,它在内存操作中返回LDAPConnection对象。 但是,在我的LDAPService中,我编写了getLdapConnection函数,该函数返回LDAPConnection,该函数用于LDAP操作。

那么如何将内存中的LDAPConnection对象传递给我的LDAPServices,以便每个操作都将内存中的LDAPConnection对象用于集成测试?

我尝试为自动装配的LDAPConnection对象创建setter方法。

//Test class
public class LdapInMem
{
    InMemoryDirectoryServer server;
    public LDAPConnection startInMemLdapServer() throws UnknownHostException
    {
           //some in memory listener config
           return ldapConnection;
    }
}
@Service
@Repository
public class LDAPService
{   
    @Autowired
    private LDAPServerConfig ldapServerConfig;

    private LDAPConnection ldapConnection;
    public LDAPConnection getLDAPConnection()
    {
          //some server config from  ldapServerConfig
          return ldapConnection;
    }
    public function()
    {
       con = getLDAPConnection();
       con.do_ldap_stuff
    }
    public function1()
    {
       con = getLDAPConnection();
       con.do_ldap_stuff1
    }
}
//LDAP service
    public void setLdapConnection(LDAPConnection ldapConnection)
    {
        this.ldapConnection = ldapConnection;
    }

我的测试用例如何在测试时从内存设置中设置LDAPService的ldapConnection对象,而在运行应用程序时通常如何从LDAPService设置? 因此,我可以在流程中测试我的功能和function1。 我希望我的API对每个LDAP操作都使用内存中的LDAPConnection对象,以测试集成流程。

接口的代码不是实现。

public interface LDAPConnectionSource {

    LDAPConnection getLDAPConnection();

}

使用Spring Profiles确定要在运行时运行哪个实现。

@Service
@Profile("integration")
public class LdapInMem implements LDAPConnectionSource
{
    InMemoryDirectoryServer server;

    @PostConstruct
    public void startInMemLdapServer() throws UnknownHostException
    {
           //some in memory listener config
    }

    public LDAPConnection getLDAPConnection() {
        return server.getConnection();  // or what ever returns a LDAPConnection
    }
}

非集成测试暗示。

@Service
@Profile("!integration")
public class LDAPService
{   
    @Autowired
    private LDAPServerConfig ldapServerConfig;

    private LDAPConnection ldapConnection;

    public LDAPConnection getLDAPConnection()
    {
          //some server config from  ldapServerConfig
          return ldapConnection;
    }
}

客户端只是自动装配接口:

public class LDAPConnectionClient {

    @Autowired
    LDAPConnectionSource source;

    public void doLDAPStuff() {
        LDAPConnection ldapConnection = source.getLDAPConnection();
        //do stuff with the connection
    }
}

有关实现的其他注意事项。 LDAPConnection线程安全吗? 如果没有,您可能需要实现一个连接池。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM