首页 > 解决方案 > 在与内存服务器集成时,如何在 Spring Boot 中执行测试?

问题描述

我正在为处理内存中 LDAP 服务器的 API 运行集成测试。有时测试会正确执行,有时则不会。为什么会这样?

我尝试过优化测试用例并减少测试用例的数量。单独每个测试都成功通过。

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

即使其他一切都很好,这些测试用例有时也会失败。为什么会这样?它与线程有关吗?在这种情况下可以做些什么来正确运行这些测试用例?

标签: springspring-bootintegration-testingmockmvcspring-test-mvc

解决方案


推荐阅读