Springboot中的test

来源:互联网 发布:mac软件怎么卸载 编辑:程序博客网 时间:2024/06/06 10:54

spring-boot-starter-test整合了spring-test和junit,简化了test,好用极了。直接贴代码讲解吧
1.maven配置:

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-test</artifactId>    <!-- 表示开发的时候引入,发布的时候不会加载此包 -->    <scope>test</scope></dependency>

这在创建Spring Initializr项目时,自带进来的test配置,也自动生成了test文件夹,文件夹下是一个test项目,自带了一个应用程序类**ApplicationTests:(@RunWith、@SpringBootTest两个标注标明了此类是测试启动类,@Test标注了测试方法)

@RunWith(SpringRunner.class)@SpringBootTestpublic class DemoApplicationTests {    @Test    public void contextLoads() {    }}

2.我们直接在这个类里添加测试方法,像开发中引用service一样,在测试方法体里去调用就可以了:
这里写图片描述
3.运行测试
在方法体中右键,选择“Run testGetUser”,即可启动测试。IDEA窗口下方会出现测试窗口。
这里写图片描述