0

I am running integration tests for my API which deals with in-memory LDAP server. Sometimes tests gets executed properly and sometimes not. Why is this happening ?

I have tried optimizing test-cases and reducing count of test-cases. Individually each test is passing successfully.

//LDAPInMem.java
public class LdapInMem {
{
function startServer()
{
    InMemoryDirectoryServer server;
    InMemoryDirectoryServerConfig config =  new InMemoryDirectoryServerConfig();
    //some server configuration code
            server.startListening();
}
}
//Integration test
import LDAPInMem
public class UserControllerIntegrationTest {

@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;

@Before
public void setup()
{
   mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
   LDAPInMem.startServer();
}
@Test
public void fun1()
{
    //some mockMvc testcase which deals with in-memory server
}
@Test
public void fun2()
{
    //some mockMvc testcase which deals with in-memory server
}
@Test
public void fun3()
{
    //some mockMvc testcase which deals with in-memory server
}
}

These test-cases are failing sometimes even though everything else is fine. Why is this happening ? Is it thread related ? What can be done in this case to run these test-cases properly?

Shubham Khandare
  • 135
  • 1
  • 15
  • You are starting the application, the server ldap server starts afterwards. Depending on the fact if it starts fast enough or not your test might fail or not. Your ldap server should be started when the applicaiton is being bootstrapped, not in the `@Before` of your test. – M. Deinum Apr 24 '19 at 13:28
  • I am not sure about this but its not about connection related issue. I guess tests are failing because of sequence of the execution ! – Shubham Khandare Apr 24 '19 at 13:33
  • @M.Deinum if you say so.what can be done to start ldap server before bootstrapping application? – Shubham Khandare Apr 24 '19 at 13:38
  • Make the embedded ldap part of your application configuration instead of starting it in your test. Put that logic in an `@Configuration` class that bootstraps the LDAP server and then uses it. – M. Deinum Apr 25 '19 at 05:47
  • I am using embedded ldap for testing only. For prod I have separate @Configuration class for LDAP server configs. – Shubham Khandare Apr 25 '19 at 07:00

0 Answers0