您的位置:首页 > 编程语言 > Java开发

Springboot中的test

2017-12-06 11:12 330 查看
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)
@SpringBootTest
public class DemoApplicationTests {
@Test
public void contextLoads() {
}
}


2.我们直接在这个类里添加测试方法,像开发中引用service一样,在测试方法体里去调用就可以了:



3.运行测试

在方法体中右键,选择“Run testGetUser”,即可启动测试。IDEA窗口下方会出现测试窗口。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: