我正在做一个没有测试的遗留项目。我们希望为我们的服务添加集成测试。服务与其他服务通信,也与数据库通信。
我可以处理运行junit测试,这样我就可以模拟服务调用和数据库调用来返回我想要的任何东西,但我想知道是否有可能运行一个实际的集成测试,在那里它与实际的开发数据库以及项目中的其他服务进行通信。
beans是在带有一些占位符的xml文件中定义的。我正在寻找一个方向,关于要寻找什么,以及在这个spring版本上是否可能。
谢谢!
发布于 2020-04-23 13:54:40
为了配置你的测试,你可以创建一流的SwitchCase并在测试类中扩展它。
import java.util.Random;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class SwitchCase{
*** here you can have tour variables and do @Autowired in yours service
}
public class YoursTestClass extends SwitchCase{
@Test
public void save() throws Exception {
}
}要做模拟,你可以使用Mockito。
@WebMvcTest
@AutoConfigureMockMvc
public class YouClass {
@Autowired
MockMvc mock
MockHttpServelet request = MockMvcRequestBuilders
.post("http/....")
.contentTupe(**pass yout content type **);
mock.perform(request)
.andExpect(MockMvcResultMatchers.status().isCreated())
.andExpect(MockMvcResultMatchers.jsonPath("id").value(7)
}这只是一个可供参考的示例,但最好先看一下mockito文档
https://stackoverflow.com/questions/61357496
复制相似问题