# mock-response-spring-boot-starter
**Repository Path**: hoowaynew/mock-response-spring-boot-starter
## Basic Information
- **Project Name**: mock-response-spring-boot-starter
- **Description**: 使用注解实现的支持多种自定义响应的简易版Mock Server组件, 使用spring-boot-starter方式构建!
- **Primary Language**: Unknown
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 1
- **Created**: 2021-12-26
- **Last Updated**: 2023-05-18
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
#
mock-response-spring-boot-starter
+ 基于spring的aop实现的mock-response-spring-boot starter组件,使得项目mock变得异常简单, 对代码侵入也较小,支持spring boot,和spirng mvc等spring相关项目, [项目地址]: https://gitee.com/hoowaynew/mock-response-spring-boot-starter
# 一. 快速开始
> spring boot项目接入
1. 添加lock starter组件依赖,目前还没上传到公共仓库,需要自己下源码build
```
org.springframework.boot
mock-response-spring-boot-starter
1.0.0-SNAPSHOT
```
2. application.yml配置全局开关, 为了防止误开启对线上造成影响, 全局开关默认是关闭的, key:spring.mock.response.enable = true
3. 在需要mock的方法上,添加注解@MockResponse(require需要设置为true, 默认false处于关闭状态),当前支持三种mock数据来源,具体如下:
- 直接在注解value字段中配置需要mock的响应json即可(简单数据可快速配置后响应回去)
- 在application.yml配置文件中配置需要相应的json数据, 在value字段设置配置文件中的路径key(如果部署运行需确保文件已经打包到服务中)
- 自己构造service进行构造数据返回, 其中在clazz字段指定该类class, method指定构造mock数据的方法名即可(如果部署运行需确保该service已经打包到服务中)
```java
package org.springframework.boot.autoconfigure.mock.test;
import org.springframework.boot.autoconfigure.mock.annotation.MockResponse;
import org.springframework.boot.autoconfigure.mock.model.MockResTypeEnum;
import org.springframework.stereotype.Service;
@Service
public class MockResService {
@MockResponse(require = true, value = "{'name': 'mock', 'age': 20, 'addr': '广东省深圳市'}", mockResTypeEnum = MockResTypeEnum.JSON)
public User getUserFromJson(String name, Integer age) {
final User user = new User();
user.setName(name);
user.setAge(age);
return user;
}
@MockResponse(require = true, value = "spring.mock.response.user", mockResTypeEnum = MockResTypeEnum.PROP)
public User getUserFromProp(String name, Integer age) {
final User user = new User();
user.setName(name);
user.setAge(age);
return user;
}
@MockResponse(require = true, clazz = MockPrepareService.class, method = "buildMockUser", mockResTypeEnum = MockResTypeEnum.METHOD)
public User getUserFromMethod(String name, Integer age) {
final User user = new User();
user.setName(name);
user.setAge(age);
return user;
}
}
```
> spring mvc项目接入
- 其他步骤和spring boot步骤一样,只需要spring-xx.xml配置中添加KlockAutoConfiguration类扫描即可,如:
```xml
```
# 二. 使用参数说明
> **application.yml配置参数说明**
```properties
spring.mock.response.enable: true/false # mock全局开关, 不配置默认关闭
spring.mock.response.xxx: '{"key": "value"}' # 需要mock的响应数据
```
> **@MockResponse注解参数说明**
```java
// @MockResponse可以标注六个参数,作用分别如下
/**
* 是否启用当前mock响应
*/
boolean require() default false;
/**
* 指定需要mock的数据路径或者json response数据
*/
String value() default "";
/**
* 获取mock响应的prepare类
*/
Class> clazz() default Void.class;
/**
* 获取mock响应的prepare类方法
*/
String method() default "";
/**
* 获取mock响应的prepare类方法参数数组
*/
String[] params() default {};
/**
* mock响应类型配置, 默认走配置文件方式响应
*/
MockResTypeEnum mockResTypeEnum() default MockResTypeEnum.PROP;
```
# 三. 关于测试
- 工程test模块下,为mock各种取值方式的测试模块。可以快速体验mock response的效果。