使用 maven 生成一个支持端到端自动测试的 RESTful 服务项目脚手架
和传统后端页面生成技术相较, RESTful 数据服务专注与数据逻辑, 而将数据呈现完全交给前端应用. 这样做可以让后端开发更加单纯, 而且更容易测试. 本文将讲述如何使用 maven 生成一个支持端到端自动测试的 RESTful 服务的项目脚手架.
1. 准备环境
如果你打算跟随本文在你的开发机上试验, 需要一下环境:
Java SDK 1.7+
maven 3.5+
如果有 IDE 就更好. 作者推荐使用 IntelliJ IDEA, 可以使用社区版, 完全免费.
2. 定义项目包和应用名称
项目包 (package) 和应用名称 (artifact) 是你的项目在 Java 依赖体系中的坐标, 即使你的项目无需被其他项目引用, 也应该给出简单明确的包和应用名字, 以便于沟通交流.
这里为我们即将生成的应用给出下面的包名和应用名:
包名: demo.restful
应用名: SimpleService
3. 生成项目脚手架
我们使用 actframework 的 archetype-simple-restful-service 来生成项目脚手架. 在命令行下
mvn archetype:generate -B \ -DgroupId=demo.restful \ -DartifactId=simple-service \ -DarchetypeGroupId=org.actframework \ -DarchetypeArtifactId=archetype-simple-restful-service \ -DarchetypeVersion=1.8.8.1
运行以上命令大致可以看到下面的 log 信息:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building Maven Stub Project (No POM) 1 [INFO] ------------------------------------------------------------------------ [INFO] [INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] [INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom --- [INFO] Generating project in Batch mode [INFO] Archetype repository not defined. Using the one from [org.actframework:archetype-simple-restful-service:1.8.7.3] found in catalog remote [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: archetype-simple-restful-service:1.8.8.0 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: demo.restful [INFO] Parameter: artifactId, Value: simple-service [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: demo.restful [INFO] Parameter: packageInPathFormat, Value: demo/restful [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: demo.restful [INFO] Parameter: groupId, Value: demo.restful [INFO] Parameter: artifactId, Value: simple-service [INFO] Executing META-INF/archetype-post-generate.groovy post-generation script [INFO] Project created from Archetype in dir: /tmp/1/simple-service [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 10.984 s [INFO] Finished at: 2018-05-13T22:15:58+10:00 [INFO] Final Memory: 25M/305M [INFO] ------------------------------------------------------------------------
4. 项目结构与内容
使用 tree 命令来查看项目结构:
simple-service/ ├── pom.xml ├── run_dev ├── run_dev.bat ├── run_e2e ├── run_e2e.bat ├── run_prod └── src ├── main │ ├── java │ │ └── demo │ │ └── restful │ │ ├── AppEntry.java │ │ └── Service.java │ └── resources │ ├── conf │ │ ├── app.properties │ │ ├── prod │ │ │ └── app.properties │ │ └── uat │ │ └── app.properties │ ├── demo │ │ └── restful │ ├── e2e │ │ └── scenarios.yml │ ├── logback.xml │ └── rythm │ └── demo │ └── restful └── test └── java └── demo
4.1 项目启动入口: AppEntry
我们看到生成的项目中有一个 AppEntry.java 文件, 打开该文件, 其内容为:
public class AppEntry { public static void main(String[] args) throws Exception { Act.start(); } }
这是一个很简单的 Java 类, 其作用是启动 ActFramework.
4.2 服务类: Service
打开本项目中的另一个 Java 文件 Service.java:
public class Service { @GetAction("hello") public String hello(@DefaultValue("World") String who) { return "Hello " + who; } @GetAction("date") public DateTime date() { return DateTime.parse("2016-03-09"); } }
代码很简单, 提供了两个 RESTful 数据服务端口:
4.2.1 GET /hello
当访问这个服务端口的时候, 应用返回 "Hello xxx" 形式的内容, 其中 xxx 是通过请求参数 who 传递的, 如果请求没有提供这个参数, 则使用默认值 World, 即, 返回 "Hello World" 字串.
4.2.2 GET /date
这个服务端口不接受参数, 直接返回一个日期数据
4.3 运行脚本
maven 为项目生成了几个可执行脚本文件:
run_dev - 以开发模式启动项目
run_dev.bat - run_dev 的 windows 版本
run_e2e - 运行端到端测试
run_e2e.bat - run_e2e 的 windows 版本
run_prod - 以产品模式启动项目
我们没有提供 run_prod 脚本的 windows 版本, 主要原因是 windows 没有默认安装支持解包 tar.gz 文件的命令行应用. 如果需要在 windows 下运行产品模式可以这样做:
运行 mvn clean package
到项目的 target/dist 目录下, 找到 tar.gz 文件, 使用第三方工具, 比如 7zip 等解包, 然后运行 run.bat 文件
5. 启动应用
下面我们使用 run_dev 脚本, 或者直接使用 mvn compile act:run 启动应用:
__ ___ _ _ __ _ _ ___ _ _ (_ | |\/| |_) | |_ __ (_ |_ |_) \ / | / |_ __) _|_ | | | |_ |_ __) |_ | \ \/ _|_ \_ |_ powered by ActFramework r1.8.8-RC4-aa2d4 version: v1.0-SNAPSHOT-180513_2237 scan pkg: base dir: /tmp/1/simple-service pid: 26126 profile: dev mode: DEV zen: Flat is better than nested. 2018-05-13 22:37:47,875 INFO a.Act@[main] - loading application(s) ... 2018-05-13 22:37:47,885 INFO a.a.App@[main] - App starting .... 2018-05-13 22:37:48,104 WARN a.h.b.ResourceGetter@[main] - URL base not exists: META-INF/resources/webjars 2018-05-13 22:37:48,119 WARN a.a.DbServiceManager@[main] - DB service not initialized: No DB plugin found 2018-05-13 22:37:48,741 WARN a.m.MailerConfig@[main] - smtp host configuration not found, will use mock smtp to send email 2018-05-13 22:37:49,033 INFO a.a.App@[main] - App[simple-service] loaded in 1148ms 2018-05-13 22:37:49,037 INFO a.a.ApiManager@[jobs-thread-3] - start compiling API book 2018-05-13 22:37:49,055 INFO o.xnio@[main] - XNIO version 3.3.8.Final 2018-05-13 22:37:49,143 INFO o.x.nio@[main] - XNIO NIO Implementation Version 3.3.8.Final 2018-05-13 22:37:49,244 INFO a.Act@[main] - network client hooked on port: 5460 2018-05-13 22:37:49,245 INFO a.Act@[main] - CLI server started on port: 5461 2018-05-13 22:37:49,246 INFO a.Act@[main] - app is ready at: http://192.168.1.2:5460 2018-05-13 22:37:49,246 INFO a.Act@[main] - it takes 2149ms to start the app
项目启动之后可以从浏览器来访问. 首先来看 GET /hello 服务端口的情况:
再请求中加入一个 who 参数再试试:
最后看看 GET /date 服务端口:
6. 测试
我们已经使用浏览器来验证项目可以正常运行, 也能得到期望的结果. 对于简单的应用来讲, 可以使用这种方式进行测试, 但随着项目的开发, 更多的服务端口会加入进来, 每次都这样来运行测试, 对开发测试人员来说是很大的负担. 下面我们先运行一下生成的 run_e2e 脚本, 或者也可以直接使用 maven 命令 mvn compile act:e2e:
Listening for transport dt_socket at address: 5005 __ ___ _ _ __ _ _ ___ _ _ (_ | |\/| |_) | |_ __ (_ |_ |_) \ / | / |_ __) _|_ | | | |_ |_ __) |_ | \ \/ _|_ \_ |_ powered by ActFramework r1.8.8-RC4-aa2d4 version: v1.0-SNAPSHOT-180513_2248 scan pkg: base dir: /tmp/1/simple-service pid: 27429 profile: e2e mode: DEV zen: Simple things should be simple, complex things should be possible. 2018-05-13 22:48:52,651 INFO a.Act@[main] - loading application(s) ... 2018-05-13 22:48:52,661 INFO a.a.App@[main] - App starting .... 2018-05-13 22:48:52,882 WARN a.h.b.ResourceGetter@[main] - URL base not exists: META-INF/resources/webjars 2018-05-13 22:48:52,895 WARN a.a.DbServiceManager@[main] - DB service not initialized: No DB plugin found 2018-05-13 22:48:53,580 WARN a.m.MailerConfig@[main] - smtp host configuration not found, will use mock smtp to send email 2018-05-13 22:48:53,910 INFO a.a.App@[main] - App[simple-service] loaded in 1249ms 2018-05-13 22:48:53,914 INFO a.a.ApiManager@[jobs-thread-2] - start compiling API book 2018-05-13 22:48:54,017 INFO o.xnio@[main] - XNIO version 3.3.8.Final 2018-05-13 22:48:54,032 INFO o.x.nio@[main] - XNIO NIO Implementation Version 3.3.8.Final 2018-05-13 22:48:54,130 INFO a.Act@[main] - network client hooked on port: 5460 2018-05-13 22:48:54,131 INFO a.Act@[main] - CLI server started on port: 5461 2018-05-13 22:48:54,132 INFO a.Act@[main] - app is ready at: http://192.168.1.2:5460 2018-05-13 22:48:54,133 INFO a.Act@[main] - it takes 2332ms to start the app Start running E2E test scenarios ================================================================================ HELLO SERVICE a service says hello -------------------------------------------------------------------------------- [PASS] send request to hello service without parameter [PASS] send request to hello servcie with parameter specified [PASS] send request to hello servcie with parameter specified and require JSON response -------------------------------------------------------------------------------- It takes 0s to run this scenario. ================================================================================ DATE SERVICE A service returns an important date in the history -------------------------------------------------------------------------------- [PASS] send request to the service [PASS] send request to the service and request response be JSON format -------------------------------------------------------------------------------- It takes 0s to run this scenario. 2018-05-13 22:48:55,224 INFO a.a.App@[jobs-thread-4] - App shutting down .... [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.874 s [INFO] Finished at: 2018-05-13T22:48:55+10:00 [INFO] Final Memory: 26M/389M [INFO] ------------------------------------------------------------------------
我们看到项目启动之后运行了 E2E 测试场景, 包括对 GET /hello 和 GET /date 的 5 个测试用例, 并且都通过了测试. 下面打开 src/main/resources/e2e/scenarios.yml 文件来看看测试场景和用例是如何定义的:
Scenario(Hello Service): description: a service says hello interactions: - description: send request to hello service without parameter request: method: GET url: /hello response: text: Hello World # response text must be "Hello World" - description: send request to hello servcie with parameter specified request: method: GET url: /hello?who=ActFramework response: # this time we demonstrate how to verify text with a list of verifiers text: - eq: Hello ActFramework # value must be equal to "Hello ActFramework" - contains: ActFramework # value must contains "ActFramework" - starts: Hello # value must starts with "Hello" - ends: Framework # value must ends with "Framework" - description: send request to hello servcie with parameter specified and require JSON response request: json: true # specify accept type is application/json method: GET url: /hello?who=Java response: json: # treat result as a JSON object # act returns json result in `{"result": ...}` style result: Hello Java # result property of the JSON object must be "Hello World" # Test Service#date() endpoint, which is available at `GET /date` endpoint Scenario(Date Service): description: A service returns an important date in the history interactions: - description: send request to the service request: method: GET url: /date response: text: - after: 1997-05-11 # the returned date should be after date 1997-05-11 - before: 13/May/2018 # the returned date should be before date 13/May/2018 - description: send request to the service and request response be JSON format request: json: true method: GET url: /date response: json: # treat result as a JSON object # act returns json result in `{"result": ...}` style result: # thus we will use `result` to fetch the date - after: 1997-05-11 # the returned date should be after date 1997-05-11 - before: 13/May/2018 # the returned date should be before date 13/May/2018
这个文件还是很容易理解, 其内容基本是按照下面的方式组织的:
首先第一层定义测试场景 Scenario, 每个 Scenario 都会给出一个名字放进圆括弧中. 我们在文件中定义了两个 Scenario
Hello Service - 测试 GET /hello 端口
Date Service - 测试 GET /date 端口
场景 (Scenario) 下面定义一个或者多个交互 Interactions
对每个交互定义请求 Request 和 响应 Response
请求 Request 的内容包括方法和 URL, 也可以指定是否要求返回 JSON 格式的请求
响应 Response 则定义期望返回内容的验证
针对每个服务端口可以写一个或多个场景, 也可以在一个场景中顺序测试多个服务端口. 完全有应用自己来定义.
7. 总结
本文介绍了如何使用 maven archetype 来生成一个可测试 RESTful 数据服务项目的脚手架, 以及如何通过定义 e2e/scenarios.yml 文件来提供自动测试的功能. 希望能够对大家带来帮助.
参考:
本文中讲述的可测试项目脚手架基于 ActFramework 构建
ActFramework 在码云上的坐标为: https://gitee.com/actframework/actframework. 欢迎参观 star, 也欢迎吐槽 ^_^
ActFramework 最新发布: https://www.oschina.net/news/96046/act-starter-1-8-8-0-released
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Spring Cloud服务提供者与服务消费者怎么运作的?
在微服务中最基本最基本的两个角色是服务提供者与服务消费者。 之前所有代码都在同一个框架的时候,比如Controller调用Service的,直接注入Service bean即可进行调用了。现在做成微服务之后,那么我们就需要有一个工程专门提供相应的服务功能,对应的有相应的工程消费这个功能,这就是服务提供者和服务消费者最基本的概念。 目录大纲: (1)服务提供者与服务消费者概念(2)编码思路(3)服务提供者编码(4)服务消费者编码(5)存在问题 接下来看下具体的内容: (1)服务提供者与服务消费者概念 服务提供者:服务的被调用方(即,为其他服务提供服务的服务); 服务消费者:服务的调用方(即,依赖其他服务的服务); (2)编码思路 这里我们使用最基本的方式实现服务提供者和服务消费者。 (1)两个项目,一个服务提供者,一个是服务消费者; (2)服务提供者从内存数据库H2中查询数据,Controller进行调用获取; (3)服务消费者调用服务提供者的Controller进行获取数据,然后返回给浏览器。 (3)服务提供者编码 新建工程 新建一个提供用户信息的工程,取名为:microserv...
- 下一篇
CMDB机柜平台结合zabbix告警展示
Zabbix3.0入门到生产环境应用实战视频教程: http://www.roncoo.com/course/view/fb3050a5b34b42f39ccad83ebebc89c1 龙果运维平台开源项目地址: https://github.com/roncoo/roncoo-cmdb 最近看了刘天斯老师的机柜展示平台,非常绚丽,而且有大屏显示的话也是能够体现运维价值的,这里就说下我最近在做的CMDB平台的一些数据: CMDB数据: 机房,机柜,机柜电源,机柜位置,机房合同,合同到期时间,机房联系人。 服务器,CPU,硬盘,是否虚拟化,宿主机,raid类型,内存。 资产ID,上架日期,下架记录,服务器代理商,代理商联系方式,服务器到保日期。 IP地址,MAC地址,业务线,产品线,操作系统。 通信这块主要技术json-rpc,然后提供Api接口给程序调用,按照固定格式导入即可;硬件固定信息,如IP,MAC,硬盘等信息,通过自动采集的API POST方式加入到数据表。像机房信息,机柜之类的可以给机房抄送一份excel表格然后直接导入即可。 机房服务器展示:通过把刘天斯老师机柜暂时平台的...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
-
Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
推荐阅读
最新文章
- CentOS7,CentOS8安装Elasticsearch6.8.6
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS6,CentOS7官方镜像安装Oracle11G
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- 设置Eclipse缩进为4个空格,增强代码规范
- Mario游戏-低调大师作品
- MySQL8.0.19开启GTID主从同步CentOS8
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果