【保姆级】SpringBoot整合Cucumber(BDD) >>> part2

有目标就不怕路远。年轻人.无论你现在身在何方.重要的是你将要向何处去。只有明确的目标才能助你成功。没有目标的航船.任何方向的风对他来说都是逆风。因此,再遥远的旅程,只要有目标.就不怕路远。没有目标,哪来的劲头?一车尔尼雷夫斯基

导读:本篇文章讲解 【保姆级】SpringBoot整合Cucumber(BDD) >>> part2,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

来一个简单的SpringBoot·查询接口·举个🌰吧~~

1. 添加依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cucumber</groupId>
    <artifactId>cucumber-java-skeleton</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>
    <name>Cucumber-Java Skeleton</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.junit</groupId>
                <artifactId>junit-bom</artifactId>
                <version>5.8.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>io.cucumber</groupId>
                <artifactId>cucumber-bom</artifactId>
                <version>7.3.4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <scope>test</scope>
        </dependency>
        
        <!-- integration with spring -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-spring</artifactId>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit-platform-engine</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.3.12.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.3.12.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.3.12.RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
            <version>9.0.7</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.10.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M6</version>
                <configuration>
                    <properties>
                        <!-- Work around. Surefire does not include enough
                             information to disambiguate between different
                             examples and scenarios. -->
                        <configurationParameters>
                            cucumber.junit-platform.naming-strategy=long
                        </configurationParameters>
                    </properties>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2. 添加各个层级的包以及相应功能的类

【保姆级】SpringBoot整合Cucumber(BDD) >>> part2

​完整代码

MyStarter.java

package com.aqin;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author aqin1012 AQin.
 * @date 2022/5/23 3:21 PM
 * @Version 1.0
 */
@SpringBootApplication
@MapperScan("com.aqin.dao.mapper")
public class MyStarter {
    public static void main(String[] args) {
        SpringApplication.run(MyStarter.class, args);
    }

    private final static String DATASOURCE_PREIFX = "spring.datasource";

    @Bean
    @ConfigurationProperties(prefix = DATASOURCE_PREIFX)
    public DataSource dataSource() {
        DriverManagerDataSource ret = new DriverManagerDataSource();
        ret.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ret.setUsername("aqin1012");
        ret.setPassword("root");
        ret.setUrl("jdbc:mysql://127.0.0.1:3306/db_ras?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC");
        return ret;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean() throws Exception {

        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));

        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}

MyController.java

package com.aqin.controller;

import com.aqin.dao.domain.MyDO;
import com.aqin.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author aqin1012 AQin.
 * @date 2022/5/23 2:24 PM
 * @Version 1.0
 */
@RestController
@RequestMapping("/MyController")
public class MyController {
    @Autowired
    private MyService myService;


    @RequestMapping(value = "/getAllData", method = RequestMethod.GET)
    public String getAllData() {
        List<MyDO> allData = myService.getAllData();
        String result = "";
        for (MyDO my : allData) {
            result += my.toString();
        }
        return result;
    }
}

MyService.java

package com.aqin.service;

import com.aqin.dao.domain.MyDO;

import java.util.List;

/**
 * @author aqin1012 AQin.
 * @date 2022/5/23 2:24 PM
 * @Version 1.0
 */
public interface MyService {
    public List<MyDO> getAllData();
}

MyServiceImpl.java

package com.aqin.service.impl;

import com.aqin.dao.MyDao;
import com.aqin.dao.domain.MyDO;
import com.aqin.dao.mapper.MyMapper;
import com.aqin.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author aqin1012 AQin.
 * @date 2022/5/23 2:24 PM
 * @Version 1.0
 */
@Service
public class MyServiceImpl implements MyService {
    @Autowired
    private MyDao myDao;

    @Override
    public List<MyDO> getAllData() {
        List<MyDO> myDOList = myDao.getAllData();
        return myDOList;
    }
}

MyDao.java

package com.aqin.dao;

import com.aqin.dao.domain.MyDO;
import com.aqin.dao.mapper.MyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author aqin1012 AQin.
 * @date 2022/5/23 2:25 PM
 * @Version 1.0
 */
@Repository
public class MyDao {
    @Autowired
    private MyMapper myMapper;

    public List<MyDO> getAllData() {
        return myMapper.getAllData();
    }

}

MyMapper.java

package com.aqin.dao.mapper;

import com.aqin.dao.domain.MyDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * @author aqin1012 AQin.
 * @date 2022/5/23 2:25 PM
 * @Version 1.0
 */
@Mapper
public interface MyMapper {
    /**
     * get all the data from cockpit
     * @return
     */
    List<MyDO> getAllData();
}

MyDO.java

package com.aqin.dao.domain;

import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * @author aqin1012 AQin.
 * @date 2022/5/23 3:04 PM
 * @Version 1.0
 */

@Data
public class MyDO implements Serializable {
    private Long id;

    private Date gmtModify;

    private Date gmtCreate;

    private Boolean deleteFlag;

    private Integer version;

    @Override
    public String toString() {
        return "MyDO{" +
                "id=" + id +
                '}';
    }
}

MyMapper.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aqin.dao.mapper.MyMapper">
    <resultMap id="BaseResultMap" type="com.aqin.dao.domain.MyDO">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="gmt_modify" jdbcType="TIMESTAMP" property="gmtModify"/>
        <result column="gmt_create" jdbcType="TIMESTAMP" property="gmtCreate"/>
        <result column="delete_flag" jdbcType="BOOLEAN" property="deleteFlag"/>
        <result column="version" jdbcType="INTEGER" property="version"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, gmt_modify, gmt_create,
        delete_flag, version
    </sql>

    <select id="getAllData" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from table
    </select>
</mapper>

application.yml

server:
  port: 8082

3. 启动测试

【保姆级】SpringBoot整合Cucumber(BDD) >>> part2

【保姆级】SpringBoot整合Cucumber(BDD) >>> part2

1. 添加我们的cucumber test

2. 修改belly.feature文件(也可以把belly.feature删了,新建一个)

  • 修改文件名

【保姆级】SpringBoot整合Cucumber(BDD) >>> part2

【保姆级】SpringBoot整合Cucumber(BDD) >>> part2

  • 修改文件内容

Feature:test API
 Scenario: test API getAllData
   Given API getAllData
   When use this api
   Then return all data

常用关键字说明

  • Feature 整体功能描述(可以简单理解为起个名)

  • Scenario 描述一个场景

  • Given 描述初始状态

  • When 描述触发事件

  • Then 描述预期结果

希望详细学习的可以参考官方文档【保姆级】SpringBoot整合Cucumber(BDD) >>> part2https://cucumber.io/docs/gherkin/reference/#example

3. 运行feature获取测试函数模版提示

PS:如果报错了,先看下面的报错处理 把3处红框框的内容复制到4处RunCucumberTest文件里(直接覆盖原先的官网用例测试即可)

【保姆级】SpringBoot整合Cucumber(BDD) >>> part2

【保姆级】SpringBoot整合Cucumber(BDD) >>> part2

​报错处理

CucumberBackendException

【保姆级】SpringBoot整合Cucumber(BDD) >>> part2

Testing started at 11:19 AM ...
五月 24, 2022 11:19:25 上午 io.cucumber.core.runtime.Runtime runFeatures
严重: Exception while executing pickle
java.util.concurrent.ExecutionException: io.cucumber.core.backend.CucumberBackendException: Please annotate a glue class with some context configuration.

For example:

   @CucumberContextConfiguration
   @SpringBootTest(classes = TestConfig.class)
   public class CucumberSpringConfiguration { }
Or: 

   @CucumberContextConfiguration
   @ContextConfiguration( ... )
   public class CucumberSpringConfiguration { }
        at java.util.concurrent.FutureTask.report(FutureTask.java:122)
        at java.util.concurrent.FutureTask.get(FutureTask.java:192)
        at io.cucumber.core.runtime.Runtime.runFeatures(Runtime.java:117)
        at io.cucumber.core.runtime.Runtime.lambda$run$0(Runtime.java:82)
        at io.cucumber.core.runtime.Runtime.execute(Runtime.java:94)
        at io.cucumber.core.runtime.Runtime.run(Runtime.java:80)
        at io.cucumber.core.cli.Main.run(Main.java:87)
        at io.cucumber.core.cli.Main.main(Main.java:30)
Caused by: io.cucumber.core.backend.CucumberBackendException: Please annotate a glue class with some context configuration.
......     
Before All/After All failed
io.cucumber.core.backend.CucumberBackendException: Please annotate a glue class with some context configuration.
......
0 Scenarios
0 Steps
0m0.557s
......
Process finished with exit code 1

解决方法

【保姆级】SpringBoot整合Cucumber(BDD) >>> part2

@Suite
@CucumberContextConfiguration
@IncludeEngines("cucumber")
@SelectClasspathResource("com/aqin")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.aqin")
public class RunCucumberTest {
}

4. 编写自己的测试代码

【保姆级】SpringBoot整合Cucumber(BDD) >>> part2

package com.aqin;

import com.aqin.controller.MyController;
import com.aqin.dao.MyDao;
import com.aqin.dao.domain.MyDO;
import com.aqin.service.MyService;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import java.util.List;

public class StepDefinitions {

    @Autowired
    private MyController myController;

    @Autowired
    private MyService myService;

    @Autowired
    private MyDao myDao;

    String allDataController = "";
    String allDataService = "";
    String allDataDao = "";

    @Given("a API:getAllData")
    public void a_api_get_all_data() {
    }

    @When("I use API:getAllData")
    public void i_use_api_get_all_data() {
        allDataController = myController.getAllData();
        List<MyDO> allDataListService = myService.getAllData();
        if (allDataListService != null) {
            for (MyDO myDO : allDataListService) {
                allDataService += myDO.toString();
            }

        }
        List<MyDO> allDataListDao = myDao.getAllData();
        if (allDataListDao != null) {
            for (MyDO myDO : allDataListDao) {
                allDataDao += myDO.toString();
            }
        }
    }

    @Then("all the data")
    public void all_the_data() {
        Assert.isTrue(allDataController.equals(allDataService), "allDataController != allDataService");
        Assert.isTrue(allDataService.equals(allDataDao), "allDataService != allDataDao");
        Assert.isTrue(allDataController.equals(allDataDao), "allDataController != allDataDao");
    }
}

5. 运行

出现下图所示即为执行成功

【保姆级】SpringBoot整合Cucumber(BDD) >>> part2

​完成✅撒花🎉🎉🎉~~~

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/135433.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!