SpringBoot热部署以及测试篇

热部署

引入依赖包

<!--热部署-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
</dependency>

热部署还可以根据实际需求来改变热部署的监控内容和方式,配置项我就不一 一介绍了

测试

依赖包

<dependency>
   <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

IDE搭建测试类

简单测试

@RunWith(SpringRunner.class)
//使用随机端口启动测试服务
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class TestBushuApplicationTests {
    @Test
    void contextLoads() {
       System.out.println("---------简单测试--------");
    }
}

Rest接口测试:使用Rest测试模版TestRestTemplate restTemplate

//Rest测试模版
    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    void contextLoads() {
        System.out.println("---------test99000--------");
        //根据具体的返回对象进行封装
        String rs= restTemplate.getForObject("/test",String.class,1L);
        System.out.println("/结果:"+rs);
    }

Mock测试

如果当前服务需要其他服务提供相关的功能,而其他服务还不能正常调度时,我们可以模拟服务返回数据进行调试。

步骤一:构造虚拟对象
步骤二:指定Mock bean对象和参数
步骤三:调用

代码如下:

    @MockBean
    private UserService userService = null;

    @Test
    public void testMock(){
        //构建虚拟对象
        User user = new User(1,"张三",new Date());
        //指定MockBean方法和参数
        BDDMockito.given(this.userService.getUserById(1)).willReturn(user);

        //进行Mock测试
        User u = userService.getUserById(1);
        System.out.println("测试返回的用户:{}"+u.toString());
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值