[jeeplus]jeeplus 中的单元测试方法论

本贴最后更新于 1854 天前,其中的信息可能已经时移世易

[初学者级别]

在刚开始接触 jeeplus 时,就一直纠结怎么用自己之前最开始的单元测试方法,对 dao 层,service 层的方法进行测试。

发现:

  1. Junit 的单元测试在 jeeplus 框架下,使用时会出现需要有 ApplicationContext ,或者需要实现模拟 请求对象等各种问题,但是 jeeplus 中的 SpringContextHolder 自己却不太会用,(或者是环境、jar 包的问题引起各种异常),不能进行常用的单元测试方法。

  2. 刚开始按照在网上找到的一些博文中的方法,也是各种尝试,同样不能解决问题。后来就搁置了好久好久。最近,实在是觉得还是非常有必要调通,会给项目的完成提供很大助力。所以,又开始找了许多博文来试、请教大佬,最终,终于调通了。

  3. 在后续的使用中,还存在着各种问题,比如 Shiro 的权限问题,还需要继续研究。

总结:

(仅简单说下大致的用法,内中详细自己还没掌握透彻,求大佬补充。)

  1. pom.xml 中需要检查是否引用了正确的 jar 包,尤其注意一些环境、jar 包的版本问题。
    另外,需要注意可能还有其他 jar 的问题。
 <!-- TEST begin -->

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>



        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>2.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.el</artifactId>
            <version>2.2.4</version>
        </dependency>


        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-websocket</artifactId>
            <version>7.0.52</version>
            <scope>test</scope>
        </dependency>


<!-- TEST end -->
  1. 创建单元测试基类
package com.jeeplus.core.junit;


import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

/**
 * Created by pc on 2019/6/3.
 */
@RunWith(SpringJUnit4ClassRunner.class)//表示整合JUnit4进行测试
@ContextConfiguration(locations={"classpath:spring/spring-context*.xml","classpath:/spring/spring-mvc*.xml"})
@WebAppConfiguration    //声明以 web 形式进行测试
public class BaseJunit4Test {
    
}

  1. 使用 Junit 开始单元测试。
    a. 创建的测试类需要继承单元测试基类。
    b. 可以正常使用 Spring 的注解方式注入 service 等。
    c. 需要注意 Jeeplus 中使用的 Shiro 权限控制问题,很多 Jeeplus 框架 中的方法可能有权限控制问题。

eg:

package com.jeeplus.modules.junittest;


import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.SpringContextHolder;
import com.jeeplus.core.junit.BaseJunit4Test;
import com.jeeplus.modules.importxlsx.entity.yifenyiduanbiao.DYifenyiduanbiao;
import com.jeeplus.modules.importxlsx.entity.zhuanyexinxibiao.DZhuanyexinxibiao;
import com.jeeplus.modules.importxlsx.service.zhuanyexinxibiao.DZhuanyexinxibiaoService;
import com.jeeplus.modules.levelformajorsfordetails.entity.LevelForMajorsDetails;
import com.jeeplus.modules.levelformajorsfordetails.entity.LevelForMajorsForDetails;
import com.jeeplus.modules.levelformajorsfordetails.mapper.LevelForMajorsDetailsMapper;
import com.jeeplus.modules.levelformajorsfordetails.service.LevelForMajorsDetailsService;
import com.jeeplus.modules.levelformajorsfordetails.service.LevelForMajorsForDetailsService;
import com.jeeplus.modules.sys.entity.User;
import org.apache.ibatis.annotations.Param;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.Basic;
import java.util.Date;
import java.util.List;

/**
 * Created by pc on 2019/6/3.
 */
public class JunitTestERecode extends BaseJunit4Test{

    @Autowired
    LevelForMajorsDetailsService levelForMajorsDetailsService;
    @Autowired
    LevelForMajorsForDetailsService levelForMajorsForDetailsService;

    @Autowired
    DZhuanyexinxibiaoService dZhuanyexinxibiaoService;


    /**
     * 需要注意的是:
     * shiro 的权限问题,Service 中大多数查询方法都有权限控制。测试时会遇到 shiro 异常。
     */

    @Before
    public void setDate(){
        
    }

    //测试 LevelForMajorsDetails 中的按更新时间删除记录
    @Test
    public void testDeleteERecord(){
        Date date = new Date();
        LevelForMajorsDetails levelForMajorsDetails = new LevelForMajorsDetails();
        levelForMajorsDetails.setUpdateDate(date);
        levelForMajorsDetails.setId(IdGen.uuid());
        levelForMajorsDetails.setIsNewRecord(true);

        if(levelForMajorsDetails != null){
            System.out.println("date:"+ date);
            levelForMajorsDetailsService.save(levelForMajorsDetails);
            System.out.println("保存成功!");
        } else {
            System.out.println("levelForMajorsDetails 为null");
        }

        List<LevelForMajorsDetails> LevelForMajorsDetailsSaved  = levelForMajorsDetailsService.findByUpdate(levelForMajorsDetails);

        for(LevelForMajorsDetails l : LevelForMajorsDetailsSaved){
            System.out.println("测试 getUpdateDate 方法中按时间获取到的记录的时间:"+l.getUpdateDate());
        }

        LevelForMajorsDetails newLevelForMajorsDetails = new LevelForMajorsDetails();
        newLevelForMajorsDetails.setUpdateDate(date);
        System.out.println("delect 的 date:"+ date);
        levelForMajorsDetailsService.deleteERecord(newLevelForMajorsDetails);
        System.out.println("执行了删除!");

    }


    //测试 LevelForMajorsForDetails 中的按更新时间删除记录
    @Test
    public void testLevelForMajorsDetailsServiceGet(){
        Date date = new Date();
        LevelForMajorsForDetails levelForMajorsForDetails = new LevelForMajorsForDetails();
        levelForMajorsForDetails.setUpdateDate(date);

        if(levelForMajorsForDetails != null){
            System.out.println("date:"+ date);
            levelForMajorsForDetailsService.save(levelForMajorsForDetails);
            System.out.println("保存成功!");
        } else {
            System.out.println("levelForMajorsDetails 为null");
        }

        List<LevelForMajorsForDetails> LevelForMajorsForDetailsSaved  = 
        levelForMajorsForDetailsService.findByUpdate(levelForMajorsForDetails);

        for(LevelForMajorsForDetails l : LevelForMajorsForDetailsSaved){
            System.out.println("测试 getUpdateDate 方法中按时间获取到的记录的时间:"+l.getUpdateDate());
        }

        LevelForMajorsForDetails newLevelForMajorsForDetails = new LevelForMajorsForDetails();
        newLevelForMajorsForDetails.setUpdateDate(date);
        System.out.println("delect 的 date:"+ date);
        levelForMajorsForDetailsService.deleteERecord(newLevelForMajorsForDetails);
        System.out.println("执行了删除!");


    }


    //测试 dZhuanyexinxibiaoService 中的 setAlreadyTransform()方法
    @Test
    public void testDZhuanyexinxibiaoService(){
        int count = dZhuanyexinxibiaoService.setAlreadyTransform();
        System.out.println("成功修改了"+ count +"条记录!");

    }



    //测试添加 beiyong20 = "" 时的查询结果是否为查询到数据库中 beiyong20=null 的记录
    @Test
    public void testFindListByBeiyong20(){
        DZhuanyexinxibiao dZhuanyexinxibiao = new DZhuanyexinxibiao();
        dZhuanyexinxibiao.setBeiyong20("");
        List<DZhuanyexinxibiao> dZhuanyexinxibiaoList =  dZhuanyexinxibiaoService.findListByBeiyong20(dZhuanyexinxibiao);

        System.out.println(dZhuanyexinxibiaoList.size());
    }

}

  1. 好吧,还是忍受不住没有 Log4j 了,找了篇大佬博文,照着配置下。
    原博文: Junit 单元测试使用 log4j 输出日志

  Junit+spring+log4j 整合之所以麻烦,是因为 spring 与 log4j 的整合,是放在 web.xml 里的,随 tomcat 启动后,spring 才会加载 log4j,而用 junit 测试是不需要 tomcat 启动的,所以 Junit 与 log4j 的整合才比较费劲。Junit 使用 spring 时,若 spring 没加载到 log4j 就会报以下警告:

1.  log4j:WARN No appenders could be found for logger(org.springframework.test.context.junit4.SpringJUnit4ClassRunner).  
2.  log4j:WARN Please initialize the log4j system properly.  
3.  log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

推荐方法 
      新建 JUnit4ClassRunner 类: 

1.  public class JUnit4ClassRunner extends SpringJUnit4ClassRunner {  
2.      static {  
3.          try {  
4.              Log4jConfigurer.initLogging("classpath:com/config/log4j.properties");  
5.          } catch (FileNotFoundException ex) {  
6.              System.err.println("Cannot Initialize log4j");  
7.          }  
8.      }  
9.      public JUnit4ClassRunner(Class<?> clazz) throws InitializationError {  
10.          super(clazz);  
11.      }  
12.  }

引用此类: 

1.  @RunWith(JUnit4ClassRunner.class)  
2.  @ContextConfiguration(locations = "classpath:com/config/springConfig.xml")  
3.  @Transactional  
4.  @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)  
5.  public class TestHibernate {  
6.      ...  
7.  }  

      这样,在启动 Junit 测试时,spring 就会加载 log4j 了。而且保持了灵活性。 

      PS:Junit 加载 spring 的 runner(SpringJUnit4ClassRunner)要优先于 spring 加载 log4j,因此采用普通方法,无法实现 spring 先加载 log4j 后被 Junit 加载。所以我们需要新建 JUnit4ClassRunner 类,修改 SpringJUnit4ClassRunner 加载 log4j 的策略。这样加载 log4j 就会优先于加载 spring 了。

采用前辈推荐的方法:

package com.jeeplus.core.junit;


import org.junit.runners.model.InitializationError;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Log4jConfigurer;

import java.io.FileNotFoundException;

/**
 * Created by pc on 2019/6/6.
 */
public class MyJUnit4ClassRunner extends SpringJUnit4ClassRunner {
    static {
        try {
            Log4jConfigurer.initLogging("classpath:properties/log4j.properties");//F:\pc\CEDS 本地SVN\jeeplus\src\main\resources\properties\log4j.properties properties/log4j.properties
        } catch (FileNotFoundException ex) {
            System.err.println("Cannot Initialize log4j");
        }
    }
    public MyJUnit4ClassRunner(Class<?> clazz) throws InitializationError {
        super(clazz);
    }
}

修改原 BaseJunit4Test 中的 @RunWith 注解中类

package com.jeeplus.core.junit;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

/**
 * Created by pc on 2019/6/3.
 */
@RunWith(MyJUnit4ClassRunner.class)//表示整合JUnit4进行测试
@ContextConfiguration(locations={"classpath:spring/spring-context*.xml","classpath:/spring/spring-mvc*.xml"})
@WebAppConfiguration    //声明以 web 形式进行测试
public class BaseJunit4Test {

}

这样就可以了。最终效果:

image.png

1 操作
PeterChu 在 2019-06-06 18:22:18 更新了该帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...
  • 其他回帖
  • PeterChu
    作者

    回复上一条第 2 点:

    public class TestEmpDAO {
    
        ApplicationContext ac ;
    
        @Before
        public void before(){
    //        ac = new ClassPathXmlApplicationContext("dispatcher-servlet.xml");  // 这样是读取不到 WEB-INF 下的 xml 文件的
            ac = new FileSystemXmlApplicationContext("web/WEB-INF/dispatcher-servlet.xml");
        }
    }
    

    Junit 单元测试 Spring 读取 WEB-INF 下 xml 文件

  • PeterChu
    作者

    单元测试中会存在 shiro 安全验证问题:

    如下图单元测试中,调用了某个 xxxService 的 findList 方法时,会报异常,但同样的情况下有时又不会报该异常。

    image.png

    org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton.  This is an invalid application configuration.
    
    	at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123)
    	at org.apache.shiro.subject.Subject$Builder.<init>(Subject.java:626)
    	at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:56)
    	at com.jeeplus.modules.sys.utils.UserUtils.getSession(UserUtils.java:290)
    	at com.jeeplus.modules.sys.utils.UserUtils.getCache(UserUtils.java:313)
    	at com.jeeplus.modules.sys.utils.UserUtils.getCache(UserUtils.java:308)
    	at com.jeeplus.modules.sys.utils.UserUtils.getDataRuleList(UserUtils.java:190)
    	at com.jeeplus.core.service.BaseService.dataRuleFilter(BaseService.java:37)
    	at com.jeeplus.core.service.CrudService.findList(CrudService.java:61)
    

    暂时没有找到原因和解决办法,先记录下。

  • PeterChu
    作者
    package com.jeeplus.modules.junittest;
    
    
    import com.alibaba.fastjson.JSONObject;
    import com.jeeplus.core.junit.BaseJunit4Test;
    import com.jeeplus.modules.university.entity.SysAreaForUniversity;
    import com.jeeplus.modules.university.service.SysAreaForUniversityService;
    import org.junit.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    
    /**
     * Created by pc on 2019/6/6.
     */
    public class TestUniversity extends BaseJunit4Test {
    
        @Autowired
        SysAreaForUniversityService sysAreaForUniversityService;
    
        //测试 SysAreaForUniversity 中的getByNameForCity() 方法
        @Test
        public void testGetByNameForCity(){
            SysAreaForUniversity sysAreaForUniversity = new SysAreaForUniversity();
            sysAreaForUniversity.setName("北京市");
            SysAreaForUniversity sa = sysAreaForUniversityService.getByNameForCity(sysAreaForUniversity);
            System.out.println("查询到的城市:"+ sa.getName() +","+ sa.getCode());
    //        String jo = JSONObject.toJSONString(sa); //此处 栈溢出 异常 
    //        System.out.println(jo);
            System.out.println(sa.getId());
    
        }
        
        
    
    }
    
    

    image.png

    还有个需要注意的地方就是:
    还没去看怎么配置下 log4j 的日志输出。
    所以,现在测试时是不能查看日志的。

  • 查看全部回帖
PeterChu
人生是场修行,求知是种信仰 ! 西安

推荐标签 标签

  • 运维

    互联网运维工作,以服务为中心,以稳定、安全、高效为三个基本点,确保公司的互联网业务能够 7×24 小时为用户提供高质量的服务。

    148 引用 • 257 回帖
  • 阿里巴巴

    阿里巴巴网络技术有限公司(简称:阿里巴巴集团)是以曾担任英语教师的马云为首的 18 人,于 1999 年在中国杭州创立,他们相信互联网能够创造公平的竞争环境,让小企业通过创新与科技扩展业务,并在参与国内或全球市场竞争时处于更有利的位置。

    43 引用 • 221 回帖 • 200 关注
  • InfluxDB

    InfluxDB 是一个开源的没有外部依赖的时间序列数据库。适用于记录度量,事件及实时分析。

    2 引用 • 59 关注
  • 宕机

    宕机,多指一些网站、游戏、网络应用等服务器一种区别于正常运行的状态,也叫“Down 机”、“当机”或“死机”。宕机状态不仅仅是指服务器“挂掉了”、“死机了”状态,也包括服务器假死、停用、关闭等一些原因而导致出现的不能够正常运行的状态。

    13 引用 • 82 回帖 • 50 关注
  • Sublime

    Sublime Text 是一款可以用来写代码、写文章的文本编辑器。支持代码高亮、自动完成,还支持通过插件进行扩展。

    10 引用 • 5 回帖
  • Gitea

    Gitea 是一个开源社区驱动的轻量级代码托管解决方案,后端采用 Go 编写,采用 MIT 许可证。

    4 引用 • 16 回帖
  • Jenkins

    Jenkins 是一套开源的持续集成工具。它提供了非常丰富的插件,让构建、部署、自动化集成项目变得简单易用。

    51 引用 • 37 回帖 • 3 关注
  • PHP

    PHP(Hypertext Preprocessor)是一种开源脚本语言。语法吸收了 C 语言、 Java 和 Perl 的特点,主要适用于 Web 开发领域,据说是世界上最好的编程语言。

    165 引用 • 407 回帖 • 514 关注
  • ActiveMQ

    ActiveMQ 是 Apache 旗下的一款开源消息总线系统,它完整实现了 JMS 规范,是一个企业级的消息中间件。

    19 引用 • 13 回帖 • 643 关注
  • 钉钉

    钉钉,专为中国企业打造的免费沟通协同多端平台, 阿里巴巴出品。

    15 引用 • 67 回帖 • 356 关注
  • danl
    89 关注
  • 尊园地产

    昆明尊园房地产经纪有限公司,即:Kunming Zunyuan Property Agency Company Limited(简称“尊园地产”)于 2007 年 6 月开始筹备,2007 年 8 月 18 日正式成立,注册资本 200 万元,公司性质为股份经纪有限公司,主营业务为:代租、代售、代办产权过户、办理银行按揭、担保、抵押、评估等。

    1 引用 • 22 回帖 • 703 关注
  • Mac

    Mac 是苹果公司自 1984 年起以“Macintosh”开始开发的个人消费型计算机,如:iMac、Mac mini、Macbook Air、Macbook Pro、Macbook、Mac Pro 等计算机。

    164 引用 • 594 回帖 • 1 关注
  • V2EX

    V2EX 是创意工作者们的社区。这里目前汇聚了超过 400,000 名主要来自互联网行业、游戏行业和媒体行业的创意工作者。V2EX 希望能够成为创意工作者们的生活和事业的一部分。

    17 引用 • 236 回帖 • 391 关注
  • DNSPod

    DNSPod 建立于 2006 年 3 月份,是一款免费智能 DNS 产品。 DNSPod 可以为同时有电信、网通、教育网服务器的网站提供智能的解析,让电信用户访问电信的服务器,网通的用户访问网通的服务器,教育网的用户访问教育网的服务器,达到互联互通的效果。

    6 引用 • 26 回帖 • 524 关注
  • Postman

    Postman 是一款简单好用的 HTTP API 调试工具。

    4 引用 • 3 回帖
  • CentOS

    CentOS(Community Enterprise Operating System)是 Linux 发行版之一,它是来自于 Red Hat Enterprise Linux 依照开放源代码规定释出的源代码所编译而成。由于出自同样的源代码,因此有些要求高度稳定的服务器以 CentOS 替代商业版的 Red Hat Enterprise Linux 使用。两者的不同在于 CentOS 并不包含封闭源代码软件。

    238 引用 • 224 回帖
  • Firefox

    Mozilla Firefox 中文俗称“火狐”(正式缩写为 Fx 或 fx,非正式缩写为 FF),是一个开源的网页浏览器,使用 Gecko 排版引擎,支持多种操作系统,如 Windows、OSX 及 Linux 等。

    7 引用 • 30 回帖 • 446 关注
  • 持续集成

    持续集成(Continuous Integration)是一种软件开发实践,即团队开发成员经常集成他们的工作,通过每个成员每天至少集成一次,也就意味着每天可能会发生多次集成。每次集成都通过自动化的构建(包括编译,发布,自动化测试)来验证,从而尽早地发现集成错误。

    14 引用 • 7 回帖
  • SVN

    SVN 是 Subversion 的简称,是一个开放源代码的版本控制系统,相较于 RCS、CVS,它采用了分支管理系统,它的设计目标就是取代 CVS。

    29 引用 • 98 回帖 • 689 关注
  • JWT

    JWT(JSON Web Token)是一种用于双方之间传递信息的简洁的、安全的表述性声明规范。JWT 作为一个开放的标准(RFC 7519),定义了一种简洁的,自包含的方法用于通信双方之间以 JSON 的形式安全的传递信息。

    20 引用 • 15 回帖 • 19 关注
  • OkHttp

    OkHttp 是一款 HTTP & HTTP/2 客户端库,专为 Android 和 Java 应用打造。

    16 引用 • 6 回帖 • 53 关注
  • OpenShift

    红帽提供的 PaaS 云,支持多种编程语言,为开发人员提供了更为灵活的框架、存储选择。

    14 引用 • 20 回帖 • 611 关注
  • 国际化

    i18n(其来源是英文单词 internationalization 的首末字符 i 和 n,18 为中间的字符数)是“国际化”的简称。对程序来说,国际化是指在不修改代码的情况下,能根据不同语言及地区显示相应的界面。

    7 引用 • 26 回帖
  • Vim

    Vim 是类 UNIX 系统文本编辑器 Vi 的加强版本,加入了更多特性来帮助编辑源代码。Vim 的部分增强功能包括文件比较(vimdiff)、语法高亮、全面的帮助系统、本地脚本(Vimscript)和便于选择的可视化模式。

    28 引用 • 66 回帖
  • 小薇

    小薇是一个用 Java 写的 QQ 聊天机器人 Web 服务,可以用于社群互动。

    由于 Smart QQ 从 2019 年 1 月 1 日起停止服务,所以该项目也已经停止维护了!

    34 引用 • 467 回帖 • 711 关注
  • IDEA

    IDEA 全称 IntelliJ IDEA,是一款 Java 语言开发的集成环境,在业界被公认为最好的 Java 开发工具之一。IDEA 是 JetBrains 公司的产品,这家公司总部位于捷克共和国的首都布拉格,开发人员以严谨著称的东欧程序员为主。

    180 引用 • 400 回帖 • 1 关注