栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

集成测试中MockMvc和RestTemplate之间的区别

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

集成测试中MockMvc和RestTemplate之间的区别

正如在说这个 文章你应该使用

MockMvc
当你想测试 服务器端 应用程序:

Spring MVC Test建立在模拟请求和响应的基础上,

spring-test
不需要运行中的servlet容器。主要区别在于,实际的Spring
MVC配置是通过TestContext框架加载的,而请求是通过实际调用
DispatcherServlet
运行时使用的以及所有相同的Spring
MVC基础结构来执行的。

例如:

@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfiguration("servlet-context.xml")public class SampleTests {  @Autowired  private WebApplicationContext wac;  private MockMvc mockMvc;  @Before  public void setup() {    this.mockMvc = webAppContextSetup(this.wac).build();  }  @Test  public void getFoo() throws Exception {    this.mockMvc.perform(get("/foo").accept("application/json"))        .andExpect(status().isOk())        .andExpect(content().mimeType("application/json"))        .andExpect(jsonPath("$.name").value("Lee"));  }}

而且

RestTemplate
你应该使用当你想测试 休息客户端 应用程序:

如果您使用编写代码

RestTemplate
,则可能需要对其进行测试,并且可以将其定位为正在运行的服务器或模拟RestTemplate。客户端REST测试支持提供了第三种选择,即使用实际值,
RestTemplate
但使用自定义配置它,以
ClientHttpRequestFactory
根据实际请求检查期望并返回存根响应。

例:

RestTemplate restTemplate = new RestTemplate();MockRestServiceServer mockServer = MockRestServiceServer.createServer(restTemplate);mockServer.expect(requestTo("/greeting"))  .andRespond(withSuccess("Hello world", "text/plain"));// use RestTemplate ...mockServer.verify();

也读这个例子



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/468843.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号