Skip to content

Commit

Permalink
增加 spring boot + feign 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed May 17, 2020
1 parent 653a27a commit 5e36ed1
Show file tree
Hide file tree
Showing 25 changed files with 571 additions and 4 deletions.
46 changes: 46 additions & 0 deletions lab-42/lab-42-demo02/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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-demo02</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> <!-- 单元测试,我们采用 H2 作为数据库 -->
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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);
}

public boolean exists(String username) {
return true;
}

public Integer add(String username, String password) {
if (exists(username)) {
return null;
}
return 1;
}

}
7 changes: 7 additions & 0 deletions lab-42/lab-42-demo02/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring:
# 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:
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
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)
@SpringBootTest
@AutoConfigureMockMvc
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")));
}

}
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package cn.iocoder.springboot.lab23.testdemo;
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cn.iocoder.springboot.lab23.testdemo.service;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest2 {

@SpyBean
private UserService userService;

@Test
public void testAddSuccess() {
System.out.println("testAddSuccess");
// Mock UserService 的 exists 方法
Mockito.when(userService.exists("username")).thenReturn(
false);

Assert.assertNotNull("注册返回为 null,注册失败",
userService.add("username", "password"));
}

@Test
public void testAddFailure() {
System.out.println("testAddFailure");

Assert.assertNull("注册返回为 null,注册失败",
userService.add("username", "password"));
}

}
7 changes: 7 additions & 0 deletions lab-42/lab-42-demo02/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
spring:
# datasource 数据源配置内容
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: sa
password:
1 change: 1 addition & 0 deletions lab-42/lab-42-demo02/src/test/resources/sql/clean.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE `t_user`
5 changes: 5 additions & 0 deletions lab-42/lab-42-demo02/src/test/resources/sql/create_tables.sql
Original file line number Diff line number Diff line change
@@ -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 '密码'
);
Loading

0 comments on commit 5e36ed1

Please sign in to comment.