springboot+mybatis+cucumber

Easter79
• 阅读 579

springboot+mybatis+cucumber springboot+mybatis+cucumber

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;


/**
 * @RunWith(Cucumber.class) 这是一个运行器 ,指用Cucumber来运行测试
 * @CucumberOptions中的features,用于指定我们项目中要运行的feature的目录
 * @CucumberOptions中的format,用于指定我们项目中要运行时生成的报告,并指定之后可以在target目录中找到对应的测试报告
 * @CucumberOptions中的glue,用于指定项目运行时查找实现step定义文件的目录
 * 
 * 在实际项目中,随着项目的进行,一个测试工程可能由多个feature文件组成,并且每个feature文件中可能也是由多个scenario组成。默认情况下,
 * 每次运行是运行所有feature中的所有scenario。这样可能导致正常情况下运行一次测试脚本,需要非常长的时间来等待测试结果。
 * 但是实际过程中,测试用例是有优先级等区分的。比如smokeTest、regressionTest等。或者有时候会有特别小部分的用例,比如等级是critical,
 * 这些用例需要长时间运行来监测系统是否没有白页或者页面404等现象。
 * 所以我们必须区分开所有的scenario,可以使我们在启动测试脚本时,可以根据我们需要来运行哪些模块的scenaro。这时我们可以使用Tags
 * 在Cucumber里Tag是直接在Feature、Scenari或Scenario Outline关键字前给feature或scenario添加任意数量的前缀为@的tags,多个tag用空格来分隔
 *
 */
@RunWith(Cucumber.class)
@CucumberOptions(        
monochrome = true,
strict = false,
features = "classpath:features/",
plugin = {"pretty","json:target/cucumber.json","html:target/test_report"}
)
public class CSAppTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

AppTest

springboot+mybatis+cucumber springboot+mybatis+cucumber

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@MapperScan(basePackages = "cn.x.x.dao.gringotts", sqlSessionFactoryRef = "gringottsSqlSessionFactory")
public class GringottsDataSourcesConfig {
    @ConfigurationProperties("spring.datasource3")
    @Bean(name = "gringottsDataSource")
    public DruidDataSource druidDataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "gringottsTransactionManager")
    public DataSourceTransactionManager customTransactionManager() {
        return new DataSourceTransactionManager(druidDataSource());
    }

    @Bean(name = "gringottsSqlSessionFactory")
    public SqlSessionFactory getSqlSessionFactory(@Qualifier("gringottsDataSource") DruidDataSource dataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        // sessionFactoryBean.setConfigLocation(new
        // ClassPathResource("mybatis-config.xml"));
        sessionFactoryBean.setDataSource(dataSource);
        return sessionFactoryBean.getObject();
    }
    
    @Bean(name = "gringottsSqlSessionTemplate")
    public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("gringottsSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
            throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

datasourceconfig

springboot+mybatis+cucumber springboot+mybatis+cucumber

<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>cn.x</groupId>
    <artifactId>x</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java8</artifactId>
            <version>4.2.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>4.2.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>4.2.0</version>
            <scope>test</scope>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.11.0</version>
        </dependency>

        <dependency>
            <groupId>com.squareup.okio</groupId>
            <artifactId>okio</artifactId>
            <version>1.14.0</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>jsr305</artifactId>
            <version>3.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.conscrypt</groupId>
            <artifactId>conscrypt-openjdk-uber</artifactId>
            <version>1.1.4</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.49</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.12</version>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
            <type>jar</type>
            <scope>system</scope>
            <systemPath>${project.basedir}/libs/ojdbc6-11.2.0.3.jar</systemPath>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

pom

springboot+mybatis+cucumber springboot+mybatis+cucumber

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;

import cucumber.api.PickleStepTestStep;
import cucumber.api.Scenario;
import cucumber.api.TestStep;
import cucumber.api.java8.Zh_cn;
import okhttp3.Response;

//@SpringBootTest(classes = CSAppTest.class)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration 
@SpringBootTest
public class RegisterStep  implements Zh_cn{
    
    Logger log = LoggerFactory.getLogger(getClass());
    
    @Value("${app.server.ip}")
    String serverIp;
    
    @Autowired
    NameGenerator nameGenerator;
    
    private JsonRequest jsonRequest;
    
    private ArrayList<String> reportStr;
    
    
    public RegisterStep() {
        当("获得一个未注册的并且前三位为(.+)的手机号码", (String mobilePrefix) -> {
            MoblieGenerator moblieGenerator= new MoblieGenerator();
            String randomMobile = moblieGenerator.getRandomMobile(mobilePrefix);
            //检查这个手机号是否已注册,获得一个未被使用过的
            while(regUtil.getMoblieCount(randomMobile) != 0) {
                randomMobile = moblieGenerator.getRandomMobile(mobilePrefix);
                log.info("while中的randomMobile:{}",randomMobile);
            }
            //放到列表中,在after中输出到报告里面
            reportStr.add(randomMobile);
            testParam.put("randomMobile", randomMobile);
            log.info("randomMobile:{}",randomMobile);
        });
        
        Before(new String[]{"@reg_test"},(Scenario scenario) -> {
            reportStr = new ArrayList<String>();
        });
            
        
        
        After(new String[]{"@reg_test"}, (Scenario scenario) -> {
            reportStr.forEach(repStr ->  scenario.embed(repStr.getBytes(Charset.forName("utf-8")),"text/plain"));
        });
    }
    
    
}

step

springboot+mybatis+cucumber springboot+mybatis+cucumber

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

@Configuration
public class DruidConfig {

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", "admin");
        reg.addInitParameter("loginPassword", "admin");
        reg.addInitParameter("logSlowSql", "true");
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        return filterRegistrationBean;
    }
}

druid

点赞
收藏
评论区
推荐文章
待兔 待兔
5个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Easter79 Easter79
3年前
vue element table 表头添加图标
!(https://oscimg.oschina.net/oscnet/189d567e07e0844612470867b06a35c0aaf.jpg)!(https://oscimg.oschina.net/oscnet/dc3b454d663d8618924b78796345a145b3f.jpg)1<template
Stella981 Stella981
3年前
Jira & Confluence 在敏捷转型中的重要性
!(https://oscimg.oschina.net/oscnet/543410bd0b319367933d992f9d80d35cd54.jpg)Atlassian产品的设计理念!(https://oscimg.oschina.net/oscnet/fac3d33081927df5f8a26204c98
Stella981 Stella981
3年前
KaliTools说明书+BurpSuit实战指南+SQL注入知识库+国外渗透报告
!(https://oscimg.oschina.net/oscnet/d1c876a571bb41a7942dd9752f68632e.gif"15254461546.gif")0X00KaliLinux Tools中文说明书!(https://oscimg.oschina.net/oscnet/
Stella981 Stella981
3年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Stella981 Stella981
3年前
Jenkins流水线即代码之扩展共享库
!(https://oscimg.oschina.net/oscnet/ab8ee75c43cb1a3fd0fac241648861b03c5.gif)!(https://oscimg.oschina.net/oscnet/1a35fdf03222f188f706711d2b43eae6a14.gif)!(https://osci
Wesley13 Wesley13
3年前
mysql系列(三)——mysql架构与存储引擎
一、MySql逻辑架构!(https://oscimg.oschina.net/oscnet/up35e1824330cd1df7ac66652b9b1dbc41965.png)1.连接层!(https://oscimg.oschina.net/oscnet/up272cb2634e262cc2df52220a063
Stella981 Stella981
3年前
Python_爬虫_案例汇总:
1.豆瓣采集!(https://oscimg.oschina.net/oscnet/29e918bf2ebced4da903f8ab789cd37ca35.jpg)!(https://oscimg.oschina.net/oscnet/83a646b5655e70dc379744e8ebfe145238d.jpg)1codin
Stella981 Stella981
3年前
Nginx快速安装
登录nginx官网,点击download!(https://oscimg.oschina.net/oscnet/cbbf95a1f35af2c4d24f0508229bf20e513.png)点击稳定版本!(https://oscimg.oschina.net/oscnet/9b1a9eb422fec4d9c4be2218261418bc
Stella981 Stella981
3年前
Eclipse 中的Maven常见报错及解决方法
1.不小心将项目中的MavenDependencies删除报错!(https://oscimg.oschina.net/oscnet/fd35e500e2580bca2afb81f35233b87a6ee.png)项目报错:!(https://oscimg.oschina.net/oscnet/8623bd4293fea39ca83a6
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k