zoukankan      html  css  js  c++  java
  • SpringBoot框架下基于Junit的单元测试

    前言

    Junit是一个Java语言的单元测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量。是一个在发展,现在已经到junit5,在javaEE开发中与很多框架相集成,使得开发者很方便。 
    这里写图片描述

    Junit常用注解:
    @Before:初始化方法
    @After:释放资源
    @Test:测试方法,在这里可以测试期望异常和超时时间
    @Ignore:忽略的测试方法
    @BeforeClass:针对所有测试,只执行一次,且必须为static void
    @AfterClass:针对所有测试,只执行一次,且必须为static void
    @RunWith:指定使用的单元测试执行类
    Junit测试用例执行顺序:
    @BeforeClass ==> @Before ==> @Test ==> @After ==> @AfterClass
    过程:就是先加载模拟的环境,再进行测试。

    测试准备
    依赖版本(不同版本存在一些差异)
    junit 4.12
    spring-test 4.3.6
    spring-boot-test 1.5.1
    添加依赖(必须)

    <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <version> 1.5.1.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version> 4.12</version>
        </dependency>

    编辑器(非必须)

    IntellijIDEA

    测试代码

    测试代码如下:

    import cn.yjxxclub.springboot.entity.Member;
    import cn.yjxxclub.springboot.mapper.MemberMapper;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.web.client.TestRestTemplate;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Author: 遇见小星
     * Email: tengxing7452@163.com
     * Date: 17-6-16
     * Time: 下午12:18
     * Describe: member应用测试类
     */
    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    public class MemberTest {
    
        /**
         * Spring RestTemplate的便利替代。你可以获取一个普通的或发送基本HTTP认证(使用用户名和密码)的模板
         * 这里不使用
         */
        @Autowired
        private TestRestTemplate testRestTemplate;
    
        @Autowired
        MemberMapper memberMapper;
    
    
        /**
         * 2017-06-16 14:08:09.884  INFO 13803 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
         size:5
         -----测试完毕-------
         2017-06-16 14:08:09.955  INFO 13803 --- [       Thread-4] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@fd07cbb: startup date [Fri Jun 16 14:08:04 CST 2017]; root of context hierarchy
         */
        @Test
        public void test(){
            Map<String,Object> map = new HashMap();
            map.put("start",0);
            map.put("size",8);
            List<Member> list = memberMapper.list(map);
            System.out.println("size:"+list.size());
            System.out.println("-----测试完毕-------");
    
        }
    }

    代码说明
    @RunWith 是junit提供的,前言已经说了
    SpringRunner是spring-test提供的测试执行单元类(SpringJUnit4ClassRunner的新名字)

    @SpringBootTest is saying “bootstrap with Spring Boot’s support”,类似springboot程序的测试引导入口
    具体请看spring.io解释:

    后记
    在springboot1.4.1以前的版本时候,网上用如下加载方式(这个方式笔者没试过,因为是aliyun的依赖库1.4.1以前的已经不支持了)

    @RunWith(SpringRunner.class)
    @SpringApplicationConfiguration(classes = SpringBootSampleApplication.class)
    public class MemberTest {
    • 在spring的其他项目中一般加载是
    @RunWith(SpringRunner.class)
    @ContextConfiguration(locations={"classpath:spring-servlet.xml", "classpath:spring-dao-test.xml", "classpath:spring-service-test.xml"})
    public class MemberTest {

    spring-boot-test还提供@DataJpaTest,@JsonTest,@JdbcTest注解,非常方便。
    不管spring提供的有多方便,还是开始说的那句话:先加载模拟的环境,再进行测试
    参考文章
    https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4
    http://www.jianshu.com/p/c0f5545f8ba3
    http://blog.csdn.net/catoop/article/details/50752964

  • 相关阅读:
    LeetCode(111) Minimum Depth of Binary Tree
    LeetCode(108) Convert Sorted Array to Binary Search Tree
    LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
    LeetCode(99) Recover Binary Search Tree
    【Android】通过经纬度查询城市信息
    【Android】自定义View
    【OpenStack Cinder】Cinder安装时遇到的一些坑
    【积淀】半夜突然有点想法
    【Android】 HttpClient 发送REST请求
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/9845051.html
Copyright © 2011-2022 走看看