0
点赞
收藏
分享

微信扫一扫

SpringBoot 测试

我是小瘦子哟 2022-02-15 阅读 12

文章目录

加载测试专用属性

优势:比多环境开发中的测试环境应县该范围更小,因对当前测试类有效

@SpringBootTest(properties = "test.prop=testValue1")
@SpringBootTest(properties = "test.prop=testValue1")
class Springboot13TestApplicationTests {

    @Value("${test.prop}")
    private String msg;

    @Test
    void contextLoads() {

        System.out.println(msg);

    }

}

加载测试专用配置

就是在测试环境中加载一个类似于分页中使用到的拦截器的bean,这里就需要使用到SpringBoot中的一个叫做@Import的注解

@Configuration
public class MsgConfig {

    @Bean
    public String msg() {

        return "bean msg";

    }

}
@SpringBootTest
@Import(MsgConfig.class)
public class ConfigurationTest {

    @Autowired
    private String msg;

    @Test
    void testString() {

        System.out.println(msg);

    }

}

在这里插入图片描述

Web环境模拟测试

这个以后补一下再,有点晚了,测试的话暂时可以使用postman进行测试的。

数据层测试回滚

在测试数据的时候,会有一些测试数据生成保存到数据库中,所以现在的情况就是,以前的方法是将maven的生命周期的test关掉,直接跳过test,现在可以添加注解进行事务回滚。
直接在测试用例上面添加@Transactional,就可以实现测试数据回滚。
在这里插入图片描述

测试用例数据设定

使用随机数据替换掉测试用例书写固定的数据。
在这里插入图片描述

举报

相关推荐

0 条评论