• 封装单元测试的重复代码


       平时几乎每个单元测试开头都要写以下代码,神烦!

     1     private Session session;
     2     private Transaction transaction;
     3 
     4     @Before
     5     public void init(){
     6         session = getSession();
     7         transaction = session.beginTransaction();
     8     }
     9 
    10     @After
    11     public void destroy(){
    12         transaction.commit();
    13         session.close();
    14     }

      今天要把它封装一下,做了一个父类:BaseTest,让所有需要上面代码的单元测试都及继承这个父类,具体内容如下:

     1 class BaseTest {
     2     static Session session;
     3     private static Transaction transaction;
     4 
     5     @Before
     6     public void init() {
     7         session = getSession();
     8         transaction = session.beginTransaction();
     9         System.out.println("[时间]"+new Date()+"    session创建成功!事务已开启!");
    10     }
    11 
    12     @After
    13     public void destroy() {
    14         transaction.commit();
    15         session.close();
    16         System.out.println("[时间]"+new Date()+"    事务已提交!session已销毁!");
    17     }
    18 }

    然后子类就可以这么写:

    public class QueryByCriteria extends BaseTest {
        @Test
        public void QueryTest(){
            Criteria criteria = session.createCriteria(ClassRoom.class);
            List<ClassRoom> list = criteria.list();
            list.stream()
                    .forEach(item->System.out.println(item.getClassName()));
        }
    }        

     最后附上HibernateUtil:

    public class HibernateUtil {
    
        private static final SessionFactory sessionFactory = buildSessionFactory();
    
        private static SessionFactory buildSessionFactory() {
            try {
                Configuration configuration = new Configuration();
                configuration.configure("Hibernate.cfg.xml");
        //        在下面添加映射,不要写在配置文件中
                configuration.addAnnotatedClass(ClassRoom.class);
                StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
                return configuration.buildSessionFactory(ssrb.build());
            }
            catch (Throwable ex) {
                System.err.println("Initial SessionFactory creation failed !" + ex);
                throw new ExceptionInInitializerError(ex);
            }
        }
    
        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    
        public static Session getSession() throws HibernateException {
            return sessionFactory.openSession();
        }
    }
  • 相关阅读:
    dinoql 试用
    dinoql 使用graphql 语法查询javascript objects
    使用git_stats 统计分析git 仓库代码&& 集成webhook
    使用gitstats分析git 仓库代码
    PostGraphile 4.4 发布,支持real time 查询
    cube.js 学习(十)cube 来自官方的学习网站
    Optimize Cube.js Performance with Pre-Aggregations
    cube.js 学习(九)cube 的pre-aggregation
    cube.js 学习(八)backend部署模式
    cube.js 学习(七)cube.js type 以及format 说明
  • 原文地址:https://www.cnblogs.com/zhang-zhi-hao/p/MakeBaseUnitTest.html
Copyright © 2020-2023  润新知