在 java Spring Boot 中,如何在集成测试中将内存中的 LDAPConnection 对象传递给 ldapService?
In java Spring Boot ,How to pass in-memory LDAPConnection object to ldapService in integration testing?
我正在为我的 API 编写集成测试。我必须测试流程,这就是我实施内存中 ldap 的原因。我使用 InMemoryDirectoryServer which return LDAPConnection 对象进行内存操作。但是在我的 LDAPService 中我写了函数 getLdapConnection which returns 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 设置?
所以我可以在流程中测试我的功能和功能1。
我希望我的 API 对每个 LDAP 操作使用内存中的 LDAPConnection 对象来测试集成流。
接口代码而非实现代码。
public interface LDAPConnectionSource {
LDAPConnection getLDAPConnection();
}
使用 Spring 配置文件来确定您希望 运行 在 运行 时执行哪个实施。
@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
线程安全吗?如果没有,您可能需要实现一个连接池。
我正在为我的 API 编写集成测试。我必须测试流程,这就是我实施内存中 ldap 的原因。我使用 InMemoryDirectoryServer which return LDAPConnection 对象进行内存操作。但是在我的 LDAPService 中我写了函数 getLdapConnection which returns 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 设置? 所以我可以在流程中测试我的功能和功能1。 我希望我的 API 对每个 LDAP 操作使用内存中的 LDAPConnection 对象来测试集成流。
接口代码而非实现代码。
public interface LDAPConnectionSource {
LDAPConnection getLDAPConnection();
}
使用 Spring 配置文件来确定您希望 运行 在 运行 时执行哪个实施。
@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
线程安全吗?如果没有,您可能需要实现一个连接池。