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

从零开始学spring-boot(5)-集成单元测试环境

2016-11-02 16:39 633 查看
 随着功能的增多,单元测试显得尤为重要,这节我主要把单元测试模块集成到项目中来。

1.首先通过Maven导入测试相关的jar包

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

2.在test文件夹下创建测试文件TestController.java
package com.zity.springboot.test;

import com.zity.springboot.Application;
import com.zity.springboot.domain.UserDomain;
import com.zity.springboot.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.util.List;

/**
* Created by Andy on 2016/11/2.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
@WebAppConfiguration
public class TestController {
@Autowired
private UserService userService;

@Test
public void getUserList(){
List<UserDomain> users=userService.findListByName("三");
for(UserDomain user:users){
System.out.println(user.getUserName());
}
}
}


3.然后运行测试方法,当看到可爱的绿色条,说明测试环境搭建成功了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐