码迷,mamicode.com
首页 > 编程语言 > 详细

springboot+mybatis+cucumber

时间:2019-01-08 12:30:15      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:login   schema   inf   att   play   lse   app   acl   pool   

技术分享图片
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
技术分享图片
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
技术分享图片
<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

 

技术分享图片
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

 

技术分享图片
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

 

springboot+mybatis+cucumber

标签:login   schema   inf   att   play   lse   app   acl   pool   

原文地址:https://www.cnblogs.com/xiaodebing/p/10237872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!