spring boot + cucumber

pom


		<dependency>
			<groupId>io.cucumber</groupId>
			<artifactId>cucumber-junit</artifactId>
			<version>4.2.2</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>io.cucumber</groupId>
			<artifactId>cucumber-spring</artifactId>
			<version>4.2.2</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>info.cukes</groupId>
			<artifactId>cucumber-java</artifactId>
			<version>1.2.5</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>info.cukes</groupId>
			<artifactId>cucumber-junit</artifactId>
			<version>1.2.5</version>
			<scope>test</scope>
		</dependency>

类WebApplicationTest

//此类写到test目录下与 WebApplication定义一模一样
@SpringBootApplication(scanBasePackages = "com")
public class WebApplicationTest  {
    public static void main(String[] args) {
        SpringApplication.run(WebApplicationTest.class, args);
    }
}

类AbstractDefs


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WebApplicationTest.class)
@SpringBootTest(classes = WebApplicationTest.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AbstractDefs {
}

类ControllerStep

public class ControllerStep extends AbstractDefs{

    //注入测试api
    @Autowired
    private TestRestTemplate testRestTemplate;

    //请求参数以及表头
    HttpEntity formEntity;

    //api返回值
    private String  response;

    //请求参数
    private String rq;

    //请求api地址
    private String api;

    //请求返回 json文件目录
    private String filePath;


    @假如("文件目录为{string}")
    public void 文件目录为(String filePath) {
        this.filePath="data/"+filePath;
    }


    @假如("请求api为{string}")
    public void 请求api为(String api)
    {
        this.api=api;
    }


    @假如("api请求为{string}")
    public void api请求为(String rqFileName) {
        rq=CommonTool.readJsonFromClassPath(filePath+"/"+rqFileName);
        HttpHeaders headers = new HttpHeaders();
        headers.set("username","cucumber-user");
        headers.set("Content-Type","application/json;charset=UTF-8");
        formEntity = new HttpEntity(rq,headers);
    }


    @当("api向服务发起请求后")
    public void api向firstsvc发起请求后() {
        response=testRestTemplate.exchange(api, HttpMethod.POST,formEntity,String.class).getBody();
    }


    @那么("服务的返回应该是{string}")
    public void firstsvc的返回应该是(String rsFileName) {
        //断言
        System.out.println(response);
        String fileRs=CommonTool.readJsonFromClassPath(filePath + "/" + rsFileName);
        Object parseResponse = JSON.parseObject(response);
        Object parseFile = JSON.parseObject(fileRs);
        assertEquals(parseResponse,parseFile);
    }

}

类RunCucumberTest


@RunWith(Cucumber.class)
@CucumberOptions( monochrome = true,plugin = {"html:target/cucumber-html-report", "json:target/cucumber/cucumber-json-report.json" },
features = "classpath:features/",tags = {"@CONTROLLER"})
public class RunCucumberTest {


}

文件 demo.feature


#language: zh-CN
@CONTROLLER
功能: 文件编号cucumber-demo
	背景:
	假如 文件目录为"cucumber-demo"

  场景大纲: <caseDetail>
    假如 请求api为"<api>"
    假如 api请求为"<request>"
    当 api向服务发起请求后
    那么 服务的返回应该是"<response>"
    例子:
      | caseDetail | api | request | response |
      |getDemon| /demo/getDemon |rq.json | rs.json|
     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值