增加 ut 示例

This commit is contained in:
YunaiV
2020-01-17 19:06:20 +08:00
parent 20c3068d54
commit d130dff29c
16 changed files with 357 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-42-demo01</artifactId>
<dependencies>
<!-- 实现对 Spring MVC 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 实现对数据库连接池的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency> <!-- 本示例,我们使用 MySQL -->
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!-- 方便等会写单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,13 @@
package cn.iocoder.springboot.lab23.testdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,33 @@
package cn.iocoder.springboot.lab23.testdemo.controller;
import cn.iocoder.springboot.lab23.testdemo.dataobject.UserDO;
import cn.iocoder.springboot.lab23.testdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 用户 Controller
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 获得指定用户编号的用户
*
* @param id 用户编号
* @return 用户
*/
@GetMapping("/get") // URL 修改成 /get
public UserDO get(@RequestParam("id") Integer id) {
// 查询并返回用户
return userService.get(id);
}
}

View File

@@ -0,0 +1,21 @@
package cn.iocoder.springboot.lab23.testdemo.dao;
import cn.iocoder.springboot.lab23.testdemo.dataobject.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
@Autowired
private JdbcTemplate template;
public UserDO selectById(Integer id) {
return template.queryForObject("SELECT id, username, password FROM t_user WHERE id = ?",
new BeanPropertyRowMapper<>(UserDO.class), // 结果转换成对应的对象
id);
}
}

View File

@@ -0,0 +1,50 @@
package cn.iocoder.springboot.lab23.testdemo.dataobject;
/**
* 用户 DO
*/
public class UserDO {
/**
* 用户编号
*/
private Integer id;
/**
* 账号
*/
private String username;
/**
* 密码(明文)
*
* ps生产环境下千万不要明文噢
*/
private String password;
public Integer getId() {
return id;
}
public UserDO setId(Integer id) {
this.id = id;
return this;
}
public String getUsername() {
return username;
}
public UserDO setUsername(String username) {
this.username = username;
return this;
}
public String getPassword() {
return password;
}
public UserDO setPassword(String password) {
this.password = password;
return this;
}
}

View File

@@ -0,0 +1,18 @@
package cn.iocoder.springboot.lab23.testdemo.service;
import cn.iocoder.springboot.lab23.testdemo.dao.UserDao;
import cn.iocoder.springboot.lab23.testdemo.dataobject.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public UserDO get(Integer id) {
return userDao.selectById(id);
}
}

View File

@@ -0,0 +1,10 @@
spring:
application:
name: demo-application
# datasource 数据源配置内容
datasource:
url: jdbc:mysql://127.0.0.1:3306/lab-39-mysql?useSSL=false&useUnicode=true&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username: root
password:

View File

@@ -0,0 +1,56 @@
package cn.iocoder.springboot.lab23.testdemo.controller;
import cn.iocoder.springboot.lab23.testdemo.dataobject.UserDO;
import cn.iocoder.springboot.lab23.testdemo.service.UserService;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
/**
* UserController 单元测试
*/
@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class UserControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private UserService userService;
@Test
public void testGet() throws Exception {
// Mock UserService 的 get 方法
Mockito.when(userService.get(1)).thenReturn(
new UserDO().setId(1).setUsername("username:1").setPassword("password:1"));
// 查询用户
ResultActions resultActions = mvc.perform(MockMvcRequestBuilders.get("/user/get?id=1"));
// 校验妆容泰马
resultActions.andExpect(MockMvcResultMatchers.status().isOk()); // 响应状态码 200
// 校验响应内容方式一:直接全部匹配
resultActions.andExpect(MockMvcResultMatchers.content().json("{\n" +
" \"id\": 1,\n" +
" \"username\": \"username:1\",\n" +
" \"password\": \"password:1\"\n" +
"}", true)); // 响应结果
// 校验响应内容方式二:逐个字段匹配
resultActions.andExpect(MockMvcResultMatchers.jsonPath("id", IsEqual.equalTo(1)));
resultActions.andExpect(MockMvcResultMatchers.jsonPath("username", IsEqual.equalTo("username:1")));
resultActions.andExpect(MockMvcResultMatchers.jsonPath("password", IsEqual.equalTo("password:1")));
}
}

View File

@@ -0,0 +1,33 @@
package cn.iocoder.springboot.lab23.testdemo.dao;
import cn.iocoder.springboot.lab23.testdemo.dataobject.UserDO;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDaoTest {
@Autowired
private UserDao userDao;
@Test
@Sql(scripts = "/sql/create_tables.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(statements = "INSERT INTO `t_user`(`id`, `username`, `password`) VALUES (1, 'username:1', 'password:1');", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/clean.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
public void testSelectById() {
// 查询用户
UserDO user = userDao.selectById(1);
// 校验结果
Assert.assertEquals("编号不匹配", 1, (int) user.getId());
Assert.assertEquals("用户名不匹配", "username:1", user.getUsername());
Assert.assertEquals("密码不匹配", "password:1", user.getPassword());
}
}

View File

@@ -0,0 +1 @@
package cn.iocoder.springboot.lab23.testdemo;

View File

@@ -0,0 +1,39 @@
package cn.iocoder.springboot.lab23.testdemo.service;
import cn.iocoder.springboot.lab23.testdemo.dao.UserDao;
import cn.iocoder.springboot.lab23.testdemo.dataobject.UserDO;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
@MockBean
private UserDao userDao;
@Autowired
private UserService userService;
@Test
public void testGet() {
// Mock UserDao 的 selectById 方法
Mockito.when(userDao.selectById(1)).thenReturn(
new UserDO().setId(1).setUsername("username:1").setPassword("password:1"));
// 查询用户
UserDO user = userService.get(1);
// 校验结果
Assert.assertEquals("编号不匹配", 1, (int) user.getId());
Assert.assertEquals("用户名不匹配", "username:1", user.getUsername());
Assert.assertEquals("密码不匹配", "password:1", user.getPassword());
}
}

View File

@@ -0,0 +1,10 @@
spring:
application:
name: demo-application
# datasource 数据源配置内容
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: sa
password:

View File

@@ -0,0 +1 @@
DROP TABLE `t_user`

View File

@@ -0,0 +1,5 @@
CREATE TABLE `t_user` (
`id` INT AUTO_INCREMENT PRIMARY KEY COMMENT '用户编号',
`username` VARCHAR(64) NOT NULL COMMENT '账号',
`password` VARCHAR(64) NOT NULL COMMENT '密码'
);

19
lab-42/pom.xml Normal file
View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>labs-parent</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-42</artifactId>
<packaging>pom</packaging>
<modules>
<module>lab-42-demo01</module>
</modules>
</project>

View File

@@ -50,6 +50,7 @@
<module>lab-39</module>
<module>lab-40</module>
<module>lab-41</module>
<module>lab-42</module>
</modules>