UT之最后一测

本文介绍了如何进行Spring和Dropwizard的集成测试。在Spring中,通过TestRestTemplate和MockMvc两种方式模拟调用REST接口,推荐使用TestRestTemplate以启动Spring容器。而在Dropwizard中,提供了ResourceExtension、启动Jetty容器以及使用Jersey HTTP client的三种测试方案。详细阐述了每种方案的优缺点及应用场景。
摘要由CSDN通过智能技术生成

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-poJoD63U-1619626601017)(https://i.loli.net/2021/04/21/CywjlKU7vbfcA4V.png)]

经过前面几次文章的分享的UT的相关知识,今天接着分享UT相关最后一测文章,希望对大家在UT的学习中有一点点的帮助。

Spring集成测试

有时候我们需要在跑起来的Spring环境中验证,Spring 框架提供了一个专门的测试模块(spring-test),用于应用程序的集成测试。

在 Spring Boot 中,你可以通过spring-boot-starter-test启动器快速开启和使用它。

这时首先就有了Spring容器运行环境,就可以模拟浏览器调用等操作

引入测试坐标

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.19.0</version>
    <scope>test</scope>
</dependency>

为了与生产环境配置区分开

新建一个application-test.yml

server:
  port: 8088 
spring:
  application:
    name: hello-service-for-test

controller类,也就是被测对象

@RestController
public class HelloController {
   

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello() throws Exception {
   
        return "Hello World";
    }
}

测试方案一

通过TestRestTemplate模拟调用Rest接口

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
class HelloControllerTest {
   

    @Autowired
    private TestRestTemplat
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值