可以在一个旧项目中开始编写单元测试了吗? [英] Feasible to start writing unit test in an old project?

查看:65
本文介绍了可以在一个旧项目中开始编写单元测试了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开始为我们的项目编写单元测试(JUnit).它将J2EE 1.4与Hibernate 3.1结合使用,并且连接代码和服务层之间存在紧密的联系(如果正确,请使用servlet?请纠正我!).

I would like to start writing unit test (JUnit) for our project. This uses J2EE 1.4 with Hibernate 3.1 and there is a tight coupling between connection code and service layer(servlets if I am right? correct me!).

因此,假设我具有保留某些表单值的功能.结构是这样的,

So suppose I have functionality to persist some form values. The structure is something like,

MyServlet.java

MyServlet.java

public void doGet(ServletRequest request, ServletResponse response)
{
    T_Donation instance - new T_Donation();

    instance.setName(request.getParameter("name"));
    instance.setAmount(request.getParameter("amount"));

    MyDAO dao = new MyDAO();
    Boolean b = dao.persistInstance(instance);

    if(b.booleanValue())
    {
        // forward to .jsp file by means of RequestDispatcher
    } 

}

T_Donation模型

Model T_Donation

public class T_Donation implements Serializable
{
   private String name;
   private String amount;

   // getters, setters
   // equals, hashcode
}

DAO类

public class MyDAO
{
    public boolean persistInstance(T_Donation instance)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = null; 
        try
        {
           tx = sesion.beginTransation();
           // setters again if needed
           session.save(instance);           
           tx.commit();
        }
        catch(Exception ex)
        {
             ex.printStackTrace();
             return false;
        } 
        finally
        {
             session.close();
        }
        return true; 
    }

}

这是代码库的方式.我正在学习DAO模式,我认为这里也错误地实现了DAO模式.

This is how the codebase is. I am learning DAO pattern and I think that too is wrongly implemented here.

所以我的问题是,考虑到这一小功能,我将如何开始编写单元测试?它将需要多少?第一步对我来说真的很难.

So my question is, considering this small functionality, how would I start writing Unit test? And how many it would require? The first steps are really hard for me.

此外,欢迎提供有关更简洁的代码实践的意见.

Also, comments about cleaner code practice are welcome.

推荐答案

您可以通过从此代码行的返回处返回一个模拟Session来模拟诸如会话之类的对象:

You can mock objects like the session by returning a mock Session from this the return of this line of code :

HibernateUtil.getSessionFactory().openSession();

根据HibernateUtil的实现方式,您可以让util在有setter的情况下返回一个模拟SessionFactory(或可以添加一个),并在测试中进行设置.

Depending on how HibernateUtil is implemented, you can either have the util return a mock SessionFactory if there is a setter (or you can add one) and set it in your tests.

HibernateUtil.setSessionFactory(SessionFactory instance);

如果您没有设置器并且无法修改代码,则可以使用Powermock之类的东西来模拟静态方法和构造函数.

If you don't have a setter and can't modify the code, then you can use something like Powermock which will allow you to mock static methods and constructors.

那是我无论如何都要开始的地方...

That's where I would start anyway...

这篇关于可以在一个旧项目中开始编写单元测试了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆