文章目录
测试文档
部署方式
打包项目
环境部署
测试文档
1、导入测试的 junit 的包
org.springframework.boot spring-boot-starter-test test 1 2 3 4 5 2、编写测试基类/**
-
服务测试抽象基类
-
Created by xsp
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public abstract class AbstractServiceTest {Long userId;
@Before
public void init() {userId = 149276L;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
这里定义了一个 通用的 userId
功能测试
留言功能测试
@Test
public void testCreateFeedback() {
Feedback appFeedback = new Feedback();
appFeedback.setUserId(userId);
appFeedback.setType(FeedbackType.APP.getCode());
appFeedback.setTemplateId("-1");
appFeedback.setComment("你这优惠券不行啊");
Response feedback1 = feedbackService.createFeedback(appFeedback);
assert feedback1.getErrorCode()!=0;
System.out.println(JSON.toJSONString(
feedbackService.createFeedback(appFeedback))
);
Feedback passFeedback = new Feedback();
passFeedback.setUserId(userId);
passFeedback.setType(FeedbackType.PASS.getCode());
passFeedback.setTemplateId("3617cf73e7a1099097242115042cb7b0");
passFeedback.setComment("优惠券评论");
Response feedback = feedbackService.createFeedback(passFeedback);
assert feedback.getErrorCode()!=0;
System.out.println(JSON.toJSONString(
feedbackService.createFeedback(passFeedback)
));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
获取 留言功能测试(在浏览版之后运行)
@After
@Test
public void testGetFeedback() {
Response feedback = feedbackService.getFeedback(userId);
assert feedback.getErrorCode()!=0;
System.out.println(JSON.toJSONString(
feedbackService.getFeedback(userId))
);
}
1
2
3
4
5
6
7
8
9
用户领取优惠券功能测试
@Test
public void testGainPassTemplate() throws Exception {
PassTemplate target = new PassTemplate();
target.setId(9);
target.setTitle("成大-2");
target.setHasToken(true);
System.out.println(JSON.toJSONString(
gainPassTemplateService.gainPassTemplate(
new GainPassTemplateRequest(userId, target)
)
));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
库存服务测试
@Test
public void testGetInventoryInfo() throws Exception {
System.out.println(JSON.toJSONString(
inventoryService.getInventoryInfo(userId),
SerializerFeature.DisableCircularReferenceDetect
));
}
1
2
3
4
5
6
7
8
Redis 客户端测试
@Test
public void testRedisTemplate() {
// redis flushall
redisTemplate.execute((RedisCallback<Object>) connection -> {
connection.flushAll(); /!!! 建议不要
return null;
});
assert redisTemplate.opsForValue().get("name") == null;
redisTemplate.opsForValue().set("name", "xsp");
assert redisTemplate.opsForValue().get("name") != null;
System.out.println(redisTemplate.opsForValue().get("name"));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
用户优惠券服务测试
分为 3个接口测试
@Test
public void testGetUserPassInfo() throws Exception {
System.out.println(JSON.toJSONString(
userPassService.getUserPassInfo(userId))
);
}
1
2
3
4
5
6
7
测试是否能获得用户信息
@Test
public void testGetUserAllPassInfo() throws Exception {
System.out.println(JSON.toJSONString(
userPassService.getUserAllPassInfo(userId)
));
}
1
2
3
4
5
6
7
测试获得用户优惠券信息
@Test
public void testUserUsePass() {
Pass pass = new Pass();
pass.setUserId(userId);
pass.setTemplateId("3617cf73e7a1099097242115042cb7b0");
System.out.println(JSON.toJSONString(
userPassService.userUsePass(pass)
));
}
1
2
3
4
5
6
7
8
9
10
11
测试获得用户用过的优惠券信息
@Test
public void testCreateUser() throws Exception {
User user = new User();
user.setBaseInfo(
new User.BaseInfo("xsp", 10, "m")
);
user.setOtherInfo(
new User.OtherInfo("123456", "成大")
);
// {"data":{"baseInfo":{"age":10,"name":"imooc","sex":"m"},
// "id":149276,
// "otherInfo":{"address":"北京市朝阳区","phone":"123456"}},
// "errorCode":0,"errorMsg":""}
System.out.println(JSON.toJSONString(userService.createUser(user)));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
测试创建用户信息
部署方式
打包项目
mvn install 打成 jar 包
环境部署
全程用 docker 部署环境
docker-compose
version: ‘3’
services:
zookeeper:
image: wurstmeister/zookeeper
container_name: zookeeper
ports:
- 2181:2181
kafka:
image: wurstmeister/kafka
container_name: kafka
hostname: ${KAFKA_ADVERTISED_HOST}
ports:
- 9092:9092
depends_on:
- zookeeper
environment:
KAFKA_ADVERTISED_HOST_NAME: ${KAFKA_ADVERTISED_HOST}
KAFKA_CREATE_TOPICS: ${KAFKA_TOPICS}
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
links:
- zookeeper
hbase:
build: ./hbase
image: lee51/hbase
container_name: hbase
hostname: ${HOSTNAME}
depends_on:
- zookeeper
links:
- zookeeper
ports:
- 16000:16000 # master
- 16010:16010 # master-ui
- 16201:16201 # rs
- 16301:16301 # rs-ui
redis:
build: ./redis/
container_name: redis
ports:
- 6379:6379
volumes:
- ./work/data/redis:/data
- ./work/conf/redis:/usr/local/etc/redis/
restart: always
mysql:
build: ./mysql/
ports:
- 3306:3306
volumes:
- ./work/conf/mysql:/etc/mysql/:rw
- ./work/data/mysql:/var/lib/mysql/:rw
- ./work/logs/mysql:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
MYSQL_DATABASE: DawnAPI
MYSQL_USER: xsp
MYSQL_PASSWORD: 123456
restart: always
command: “–character-set-server=utf8”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
部署文档目录
│ docker-compose.yml
│ start-all.sh
│
├─hbase # hbase 镜像文件
│ Dockerfile
│ hbase-env.sh
│ hbase-site.xml
│ start-hbase.sh
│
├─mysql # mysql 镜像文件
│ Dockerfile
│
├─redis # redis 镜像文件
│ Dockerfile
│ redis.conf
│
└─work # docker 工作目录,主要包括 mysql 和 redis 的配置目录
├─conf
│ ├─mysql
│ │ │ my.cnf
│ │ │ my.cnf.fallback
│ │ │ mysql.cnf
│ │ │
│ │ ├─conf.d
│ │ │ docker.cnf
│ │ │ mysql.cnf
│ │ │ mysqldump.cnf
│ │ │
│ │ └─mysql.conf.d
│ │ mysqld.cnf
│ │
│ └─redis
│ │ demo.php
│ │ redis.conf
│ └─redis
│ dump.rdb
│
└─logs # 日志记录
├─mysql
├─nginx
│ access.log
│ error.log
│ host.access.log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
在命令行中运行
./start.sh
其命令行代码如下
#!/bin/bash -eu
SHOULD_BUILD=$(docker images | grep -c ‘lee51/hbase’ || echo)
if [[ $SHOULD_BUILD -lt 1 ]]; then
docker-compose build
fi
export HOSTNAME
export KAFKA_ADVERTISED_HOST=$(hostname)
docker-compose up -V
1
2
3
4
5
6
7
8
9
10
在运行前,向mysql,hbase中写入我们需要的表和数据
在Hbase 中插入
docker exec -it hbase sh
create_namespace ‘pb’
create ‘pb:user’, {NAME => ‘b’, VERSIONS => ‘3’, TTL => ‘2147483647’, ‘BLOOMFILTER’ => ‘ROW’}, {NAME => ‘o’, VERSIONS => ‘3’, TTL => ‘2147483647’, ‘BLOOMFILTER’ => ‘ROW’}
create ‘pb:pass’, {NAME => ‘i’, VERSIONS => ‘3’, TTL => ‘2147483647’, ‘BLOOMFILTER’ => ‘ROW’}
create ‘pb:passtemplate’, {NAME => ‘b’, VERSIONS => ‘3’, TTL => ‘2147483647’, ‘BLOOMFILTER’ => ‘ROW’}, {NAME => ‘c’, VERSIONS => ‘3’, TTL => ‘2147483647’, ‘BLOOMFILTER’ => ‘ROW’}
create ‘pb:feedback’, {NAME => ‘i’, VERSIONS => ‘3’, TTL => ‘2147483647’, ‘BLOOMFILTER’ => ‘ROW’}
1
2
3
4
5
6
7
在 mysql 中插入对应的表
docker exec -it mysql sh