springboot+mybatis+cucumber

1import org.junit.runner.RunWith; 2 3import cucumber.api.CucumberOptions; 4import cucumber.api.junit.Cucumber; 5 6 7/** 8 * @RunWith(Cucumber.class) 这是一个运行器 ,指用Cucumber来运行测试 9 * @CucumberOptions中的features,用于指定我们项目中要运行的feature的目录 10 * @CucumberOptions中的format,用于指定我们项目中要运行时生成的报告,并指定之后可以在target目录中找到对应的测试报告 11 * @CucumberOptions中的glue,用于指定项目运行时查找实现step定义文件的目录 12 * 13 * 在实际项目中,随着项目的进行,一个测试工程可能由多个feature文件组成,并且每个feature文件中可能也是由多个scenario组成。默认情况下, 14 * 每次运行是运行所有feature中的所有scenario。这样可能导致正常情况下运行一次测试脚本,需要非常长的时间来等待测试结果。 15 * 但是实际过程中,测试用例是有优先级等区分的。比如smokeTest、regressionTest等。或者有时候会有特别小部分的用例,比如等级是critical, 16 * 这些用例需要长时间运行来监测系统是否没有白页或者页面404等现象。 17 * 所以我们必须区分开所有的scenario,可以使我们在启动测试脚本时,可以根据我们需要来运行哪些模块的scenaro。这时我们可以使用Tags 18 * 在Cucumber里Tag是直接在Feature、Scenari或Scenario Outline关键字前给feature或scenario添加任意数量的前缀为@的tags,多个tag用空格来分隔 19 * 20 */ 21@RunWith(Cucumber.class) 22@CucumberOptions( 23monochrome = true, 24strict = false, 25features = "classpath:features/", 26plugin = {"pretty","json:target/cucumber.json","html:target/test_report"} 27) 28public class CSAppTest { 29 30 public static void main(String[] args) { 31 // TODO Auto-generated method stub 32 33 } 34 35}

AppTest

1import org.apache.ibatis.session.SqlSessionFactory; 2import org.mybatis.spring.SqlSessionFactoryBean; 3import org.mybatis.spring.SqlSessionTemplate; 4import org.mybatis.spring.annotation.MapperScan; 5import org.springframework.beans.factory.annotation.Qualifier; 6import org.springframework.boot.context.properties.ConfigurationProperties; 7import org.springframework.context.annotation.Bean; 8import org.springframework.context.annotation.Configuration; 9import org.springframework.jdbc.datasource.DataSourceTransactionManager; 10 11import com.alibaba.druid.pool.DruidDataSource; 12 13@Configuration 14@MapperScan(basePackages = "cn.x.x.dao.gringotts", sqlSessionFactoryRef = "gringottsSqlSessionFactory") 15public class GringottsDataSourcesConfig { 16 @ConfigurationProperties("spring.datasource3") 17 @Bean(name = "gringottsDataSource") 18 public DruidDataSource druidDataSource() { 19 return new DruidDataSource(); 20 } 21 22 @Bean(name = "gringottsTransactionManager") 23 public DataSourceTransactionManager customTransactionManager() { 24 return new DataSourceTransactionManager(druidDataSource()); 25 } 26 27 @Bean(name = "gringottsSqlSessionFactory") 28 public SqlSessionFactory getSqlSessionFactory(@Qualifier("gringottsDataSource") DruidDataSource dataSource) 29 throws Exception { 30 final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); 31 // sessionFactoryBean.setConfigLocation(new 32 // ClassPathResource("mybatis-config.xml")); 33 sessionFactoryBean.setDataSource(dataSource); 34 return sessionFactoryBean.getObject(); 35 } 36 37 @Bean(name = "gringottsSqlSessionTemplate") 38 public SqlSessionTemplate getSqlSessionTemplate(@Qualifier("gringottsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) 39 throws Exception { 40 return new SqlSessionTemplate(sqlSessionFactory); 41 } 42}

datasourceconfig

1<project xmlns="http://maven.apache.org/POM/4.0.0" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>cn.x</groupId> 6 <artifactId>x</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 9 <parent> 10 <groupId>org.springframework.boot</groupId> 11 <artifactId>spring-boot-starter-parent</artifactId> 12 <version>2.1.0.RELEASE</version> 13 <relativePath /> <!-- lookup parent from repository --> 14 </parent> 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 19 <java.version>1.8</java.version> 20 </properties> 21 22 <dependencies> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-web</artifactId> 26 </dependency> 27 28 <dependency> 29 <groupId>io.cucumber</groupId> 30 <artifactId>cucumber-java8</artifactId> 31 <version>4.2.0</version> 32 <scope>test</scope> 33 </dependency> 34 35 <dependency> 36 <groupId>io.cucumber</groupId> 37 <artifactId>cucumber-spring</artifactId> 38 <version>4.2.0</version> 39 <scope>test</scope> 40 </dependency> 41 42 <dependency> 43 <groupId>io.cucumber</groupId> 44 <artifactId>cucumber-junit</artifactId> 45 <version>4.2.0</version> 46 <scope>test</scope> 47 </dependency> 48 49 <dependency> 50 <groupId>org.springframework.boot</groupId> 51 <artifactId>spring-boot-starter-test</artifactId> 52 <scope>test</scope> 53 </dependency> 54 55 <dependency> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-starter-jdbc</artifactId> 58 </dependency> 59 60 <dependency> 61 <groupId>org.mybatis.spring.boot</groupId> 62 <artifactId>mybatis-spring-boot-starter</artifactId> 63 <version>1.3.2</version> 64 </dependency> 65 66 <dependency> 67 <groupId>com.squareup.okhttp3</groupId> 68 <artifactId>okhttp</artifactId> 69 <version>3.11.0</version> 70 </dependency> 71 72 <dependency> 73 <groupId>com.squareup.okio</groupId> 74 <artifactId>okio</artifactId> 75 <version>1.14.0</version> 76 </dependency> 77 78 <dependency> 79 <groupId>com.google.code.findbugs</groupId> 80 <artifactId>jsr305</artifactId> 81 <version>3.0.2</version> 82 </dependency> 83 84 <dependency> 85 <groupId>org.conscrypt</groupId> 86 <artifactId>conscrypt-openjdk-uber</artifactId> 87 <version>1.1.4</version> 88 </dependency> 89 90 <dependency> 91 <groupId>com.alibaba</groupId> 92 <artifactId>fastjson</artifactId> 93 <version>1.2.49</version> 94 </dependency> 95 <dependency> 96 <groupId>com.alibaba</groupId> 97 <artifactId>druid</artifactId> 98 <version>1.1.12</version> 99 </dependency> 100 101 <dependency> 102 <groupId>mysql</groupId> 103 <artifactId>mysql-connector-java</artifactId> 104 <version>8.0.12</version> 105 </dependency> 106 <dependency> 107 <groupId>com.oracle</groupId> 108 <artifactId>ojdbc6</artifactId> 109 <version>11.2.0.3</version> 110 <type>jar</type> 111 <scope>system</scope> 112 <systemPath>${project.basedir}/libs/ojdbc6-11.2.0.3.jar</systemPath> 113 </dependency> 114 115 </dependencies> 116 117 <build> 118 <plugins> 119 <plugin> 120 <groupId>org.springframework.boot</groupId> 121 <artifactId>spring-boot-maven-plugin</artifactId> 122 </plugin> 123 </plugins> 124 </build> 125 126</project>

pom

1import java.nio.charset.Charset; 2import java.util.ArrayList; 3import java.util.Collection; 4import java.util.HashMap; 5import java.util.List; 6import java.util.Random; 7 8import org.junit.runner.RunWith; 9import org.slf4j.Logger; 10import org.slf4j.LoggerFactory; 11import org.springframework.beans.factory.annotation.Autowired; 12import org.springframework.beans.factory.annotation.Value; 13import org.springframework.boot.test.context.SpringBootTest; 14import org.springframework.context.annotation.Bean; 15import org.springframework.context.annotation.Scope; 16import org.springframework.test.context.ContextConfiguration; 17import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 18 19import com.alibaba.fastjson.JSON; 20import com.alibaba.fastjson.JSONPath; 21 22import cucumber.api.PickleStepTestStep; 23import cucumber.api.Scenario; 24import cucumber.api.TestStep; 25import cucumber.api.java8.Zh_cn; 26import okhttp3.Response; 27 28//@SpringBootTest(classes = CSAppTest.class) 29@RunWith(SpringJUnit4ClassRunner.class) 30@ContextConfiguration 31@SpringBootTest 32public class RegisterStep implements Zh_cn{ 33 34 Logger log = LoggerFactory.getLogger(getClass()); 35 36 @Value("${app.server.ip}") 37 String serverIp; 38 39 @Autowired 40 NameGenerator nameGenerator; 41 42 private JsonRequest jsonRequest; 43 44 private ArrayList<String> reportStr; 45 46 47 public RegisterStep() { 48 ("获得一个未注册的并且前三位为(.+)的手机号码", (String mobilePrefix) -> { 49 MoblieGenerator moblieGenerator= new MoblieGenerator(); 50 String randomMobile = moblieGenerator.getRandomMobile(mobilePrefix); 51 //检查这个手机号是否已注册,获得一个未被使用过的 52 while(regUtil.getMoblieCount(randomMobile) != 0) { 53 randomMobile = moblieGenerator.getRandomMobile(mobilePrefix); 54 log.info("while中的randomMobile:{}",randomMobile); 55 } 56 //放到列表中,在after中输出到报告里面 57 reportStr.add(randomMobile); 58 testParam.put("randomMobile", randomMobile); 59 log.info("randomMobile:{}",randomMobile); 60 }); 61 62 Before(new String[]{"@reg_test"},(Scenario scenario) -> { 63 reportStr = new ArrayList<String>(); 64 }); 65 66 67 68 After(new String[]{"@reg_test"}, (Scenario scenario) -> { 69 reportStr.forEach(repStr -> scenario.embed(repStr.getBytes(Charset.forName("utf-8")),"text/plain")); 70 }); 71 } 72 73 74}

step

1import org.springframework.boot.web.servlet.FilterRegistrationBean; 2import org.springframework.boot.web.servlet.ServletRegistrationBean; 3import org.springframework.context.annotation.Bean; 4import org.springframework.context.annotation.Configuration; 5 6import com.alibaba.druid.support.http.StatViewServlet; 7import com.alibaba.druid.support.http.WebStatFilter; 8 9@Configuration 10public class DruidConfig { 11 12 @Bean 13 public ServletRegistrationBean druidServlet() { 14 ServletRegistrationBean reg = new ServletRegistrationBean(); 15 reg.setServlet(new StatViewServlet()); 16 reg.addUrlMappings("/druid/*"); 17 reg.addInitParameter("loginUsername", "admin"); 18 reg.addInitParameter("loginPassword", "admin"); 19 reg.addInitParameter("logSlowSql", "true"); 20 return reg; 21 } 22 23 @Bean 24 public FilterRegistrationBean filterRegistrationBean() { 25 FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); 26 filterRegistrationBean.setFilter(new WebStatFilter()); 27 filterRegistrationBean.addUrlPatterns("/*"); 28 filterRegistrationBean.addInitParameter("profileEnable", "true"); 29 return filterRegistrationBean; 30 } 31}

druid

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

vue element table 表头添加图标

!(https://oscimg.oschina.net/oscnet/189d567e07e0844612470867b06a35c0aaf.jpg)!(https://oscimg.oschina.net/oscnet/dc3b454d663d8618924b78796345a145b3f.jpg)1<template

Jira & Confluence 在敏捷转型中的重要性

!(https://oscimg.oschina.net/oscnet/543410bd0b319367933d992f9d80d35cd54.jpg)Atlassian产品的设计理念!(https://oscimg.oschina.net/oscnet/fac3d33081927df5f8a26204c98

KaliTools说明书+BurpSuit实战指南+SQL注入知识库+国外渗透报告

!(https://oscimg.oschina.net/oscnet/d1c876a571bb41a7942dd9752f68632e.gif"15254461546.gif")0X00KaliLinux Tools中文说明书!(https://oscimg.oschina.net/oscnet/

Android So动态加载 优雅实现与原理分析

背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我

Jenkins流水线即代码之扩展共享库

!(https://oscimg.oschina.net/oscnet/ab8ee75c43cb1a3fd0fac241648861b03c5.gif)!(https://oscimg.oschina.net/oscnet/1a35fdf03222f188f706711d2b43eae6a14.gif)!(https://osci