从零开始学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.然后运行测试方法,当看到可爱的绿色条,说明测试环境搭建成功了。
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.然后运行测试方法,当看到可爱的绿色条,说明测试环境搭建成功了。
相关文章推荐
- 81. Spring Boot集成JSP疑问【从零开始学Spring Boot】
- 83. Spring Boot 1.4单元测试【从零开始学Spring Boot】
- 24. Spring Boot环境变量读取和属性对象的绑定【从零开始学Spring Boot】
- spring boot 、springMVC环境集成百度ueditor富文本编辑器,使用七牛云存储图片
- 35. Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
- 52. spring boot日志升级篇—log4j多环境不同日志级别的控制【从零开始学Spring Boot】
- 52. spring boot日志升级篇—log4j多环境不同日志级别的控制【从零开始学Spring Boot】(
- spring boot单元测试restfull API【从零开始学Spring Boot】
- 37. Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】
- 从零开始学spring-boot(3)-集成logback日志
- 37. Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】
- 48. spring boot单元测试restfull API【从零开始学Spring Boot】
- 35. Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
- 27. Spring Boot Junit单元测试【从零开始学Spring Boot】
- (33)Spring Boot 监控和管理生产环境【从零开始学Spring Boot】
- 85. Spring Boot集成RabbitMQ【从零开始学Spring Boot】
- 从零开始学spring-boot(4)-集成Mybatis
- 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】
- 52. spring boot日志升级篇—log4j多环境不同日志级别的控制【从零开始学Spring Boot】
- 33. Spring Boot 监控和管理生产环境【从零开始学Spring Boot】