0

I am writing integration tests for my API. I have to test flow that's why I implemented in-memory ldap. I have used InMemoryDirectoryServer which return LDAPConnection object for in memory operations. But in my LDAPService I have written function getLdapConnection which returns LDAPConnection, this function is being used for LDAP operations.

so how do I pass in-memory LDAPConnection object to my LDAPServices so each operation will take in-memory LDAPConnection object for integration testing?

I have tried creating setter method for autowired LDAPConnection object.

//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;
    }

how can my testcases set ldapConnection object of LDAPService from in-memory setup while testing and normally from LDAPService while running app? So I can test my function and function1 in the flow. I expect my API to use in-memory LDAPConnection object for every LDAP operation to test integration flow.

Shubham Khandare
  • 135
  • 1
  • 15

1 Answers1

1

Code to interfaces not implementations.

public interface LDAPConnectionSource {

    LDAPConnection getLDAPConnection();

}

Use Spring Profiles to determine which implementation you want to run at runtime.

@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
    }
}

Non integration test impl.

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

    private LDAPConnection ldapConnection;

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

The client just Autowires the interface:

public class LDAPConnectionClient {

    @Autowired
    LDAPConnectionSource source;

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

Some other things to consider about your implementation. Is LDAPConnection thread safe? If not, you will probably need to implement a connection pool.

MarkOfHall
  • 3,334
  • 1
  • 26
  • 30