#yyds干货盘点# springboot使用@SpringBootTest注解进行单元测试

Posted 灰太狼_cxh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了#yyds干货盘点# springboot使用@SpringBootTest注解进行单元测试相关的知识,希望对你有一定的参考价值。

springboot使用@SpringBootTest注解进行单元测试

1.代码实现:

添加依赖

 <!-- spring boot web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 视图 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<!-- mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- mysql 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.4</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>

控制层

@RestController
public class UserController

@Autowired
private IUserService userService;

@GetMapping("/getAll")
public List<User> getAll()
return userService.getAll();


@GetMapping("/getUserById")
public User getUserById(Integer id)
return userService.getUserById(id);

服务层

public interface IUserService 

// 获取所有用户
List<User> getAll();

// 根据id获取用户
User getUserById(Integer id);
@Service("userServiceImpl")
public class UserServiceImpl implements IUserService

@Autowired
private IUserMapper userMapper;

@Override
public List<User> getAll()
return userMapper.getAll();


@Override
public User getUserById(Integer id)
return userMapper.getUserById(id);


数据访问层

@Mapper
public interface IUserMapper
//获取所有用户
List<User>getAll();
//根据id获取用户
User getUserById(Integer id);
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cxh.study.platform.mapper.IUserMapper" >
<!--获取所有用户 -->
<select id="getAll" resultType="com.cxh.study.platform.entity.User">
SELECT * FROM s_user
</select>
<!-- 根据id获取单个用户 -->
<select id="getUserById" parameterType="java.lang.Integer" resultType="com.cxh.study.platform.entity.User">
SELECT * FROM s_user
where id=#id
</select>
</mapper>

测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = GeneratorApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserTest

/**
* @LocalServerPort 提供了 @Value("$local.server.port") 的代替
*/
@LocalServerPort
private int port;

private URL base;

@Autowired
private TestRestTemplate restTemplate;

@Before
public void setUp() throws Exception
String url = String.format("http://localhost:%d/", port);
System.out.println(String.format("port is : [%d]", port));
this.base = new URL(url);



//@Test
public void getAllTest() throws Exception

ResponseEntity<List> response = this.restTemplate.getForEntity(
this.base.toString() + "/getAll", List.class, "");
System.out.println(String.format("测试结果为:%s", response.getBody()));


@Test
public void getUserByIdTest() throws Exception

ResponseEntity<User> response = this.restTemplate.getForEntity(
this.base.toString() + "/getUserById?id=1", User.class, "");
System.out.println(String.format("测试结果为:%s", response.getBody().toString()));


​其中,classes属性指定启动类,SpringBootTest.WebEnvironment.RANDOM_PORT经常和测试类中@LocalServerPort一起在注入属性时使用。会随机生成一个端口号。

2.实现效果:

运行测试方法getUserByIdTest

测试结果为:User [id=1, account=cxh, password=123, type=1, status=1]

以上是关于#yyds干货盘点# springboot使用@SpringBootTest注解进行单元测试的主要内容,如果未能解决你的问题,请参考以下文章

#yyds干货盘点# springboot配置@Async异步任务的线程池

#yyds干货盘点# springboot使用@SpringBootTest注解进行单元测试

#yyds干货盘点#jackson学习之九:springboot整合(配置文件)

SpringBoot | 3.2 整合MyBatis #yyds干货盘点#

#yyds干货盘点# springboot整合Oauth2,GateWay实现网关登录授权验证

#yyds干货盘点# springboot整合Elastic Job实现分片配置定时任务