首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置我的项目来运行与Spring 3.2.4版本的集成测试?

如何设置我的项目来运行与Spring 3.2.4版本的集成测试?
EN

Stack Overflow用户
提问于 2020-04-22 12:48:28
回答 1查看 18关注 0票数 0

我正在做一个没有测试的遗留项目。我们希望为我们的服务添加集成测试。服务与其他服务通信,也与数据库通信。

我可以处理运行junit测试,这样我就可以模拟服务调用和数据库调用来返回我想要的任何东西,但我想知道是否有可能运行一个实际的集成测试,在那里它与实际的开发数据库以及项目中的其他服务进行通信。

beans是在带有一些占位符的xml文件中定义的。我正在寻找一个方向,关于要寻找什么,以及在这个spring版本上是否可能。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-04-23 13:54:40

为了配置你的测试,你可以创建一流的SwitchCase并在测试类中扩展它。

代码语言:javascript
复制
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。

代码语言:javascript
复制
@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文档

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61357496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档