pom
<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <scope>test</scope> <version>1.2.4</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-jvm</artifactId> <version>1.2.4</version> <type>pom</type> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-core</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-spring</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.2.4</version> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> </dependency>
在test目录下建一个resources目录,在该目录下建一个test.feature的文件
该文件内容如下
Feature: 登录系统 Scenario: 登录系统 Given 我以"admin"登录,密码为"admin",获取token And 用获取的token查看当前登录用户
这些关键字代表一定的测试目的和意义,由框架限制的,具体内容如下
#language: zh-CN
#"zh-CN": {
# "but": "*|但是<",
# "and": "*|而且<|并且<|同时<",
# "then": "*|那么<",
# "when": "*|当<",
# "name": "Chinese simplified",
# "native": "简体中文",
# "feature": "功能",
# "background": "背景",
# "scenario": "场景|剧本",
# "scenario_outline": "场景大纲|剧本大纲",
# "examples": "例子",
# "given": "*|假如<|假设<|假定<"
# }
此时该文件的内容是带有泛白颜色的
我们用鼠标点中泛白的地方,点击Alt+Entry(Windows)或者option+Entry(mac)可以依次生成MyStepdefs.java的文件
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @SpringBootTest(classes = {UserCenterApplication.class}) public class MyStepdefs { @Autowired private TokenController tokenController; @Autowired private RestTemplate restTemplate; private String token; @Given("^我以\"([^\"]*)\"登录,密码为\"([^\"]*)\",获取token$") public void 我以登录密码为获取token(String arg0, String arg1) throws Throwable { Result<Map> result = tokenController.login(arg0, arg1); System.out.println("token结果为:" + JSONObject.toJSONString(result)); token = ((Map)result.getData().get("token_info")).get("access_token").toString(); System.out.println(token); } @And("^用获取的token查看当前登录用户$") public void 用获取的token查看当前登录用户() throws Throwable { AppUser user = restTemplate.getForObject("http://172.10.40.179:8001/users/current?access_token=" + token, AppUser.class); System.out.println(JSONObject.toJSONString(user)); } }
最后运行这个测试类
@RunWith(Cucumber.class) @CucumberOptions(features = "src/test/resources") public class DemoRun { }
运行结果
token结果为:{"code":200,"data":{"token_info":{"access_token":"6fa8ac20-7961-441f-bbe1-fe357a8f6e6a","token_type":"bearer","refresh_token":"6defb63b-9e6c-4707-81bd-6e4c5ed6bb99","expires_in":23038,"scope":"app"}},"msg":"操作成功"}
6fa8ac20-7961-441f-bbe1-fe357a8f6e6a
{"createTime":1516147200000,"enabled":true,"id":1,"nickname":"测试1","password":"$2a$10$3uOoX1ps14CxuotogUoDreW8zXJOZB9XeGdrC/xDV36hhaE8Rn9HO","sex":1,"type":"APP","updateTime":1516147200000,"username":"admin"}
1 Scenarios (1 passed)
2 Steps (2 passed)
0m0.992s