在J2ee应用程序上为DAO编写测试用例 [英] writing test case for a DAO on a J2ee Application

查看:111
本文介绍了在J2ee应用程序上为DAO编写测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为J2EE应用程序中的DAO类编写一些测试用例。我的DAO类中的方法尝试基于JDBC URL(位于应用服务器上)与数据库建立连接。所以从前端如果我点击一堆东西并使DAO触发它运行正常。但是,当我为DAO编写测试用例并且DAO对象调用该方法时,它无法获得与数据库的连接。我认为,因为JDBC资源在App服务器上,这就是为什么它不能从测试类中运行。

I am trying to write some test cases for my DAO classes in a J2EE applications. Methods in my DAO classes try to get connection to the Database based on a JDBC URL (which is on the app server). So from the front end if I click bunch of stuff and make the DAO trigger it runs fine. However, when I write tests cases for the DAO and the DAO object calls the method then it is not able to get the connection to the database. I think since the JDBC resource is on the App server that is why it is not working from the test class.

因为这个当我运行我的测试而不是传递或失败时..返回一堆错误。

because of this when I run my tests instead of pass or fail..it returns bunch of errors.

有人遇到此问题吗?我该怎么做才能克服这个问题?

Has someone encountered this issue? what can I do to overcome this?

示例:

public class DBConnectionManager {
   public static final String DB_URL = "jdbc/RSRC/my/connection/mydb"
   public Connection getconnection ()
   {
     DataSource ds = ServiceLocator.getInstance().getDataSource(DB_URL);
     return ds.getconnection();
   } 
}
public class MyDAO extends DBConnectionManager {
    publci SomeBean getContents (String id)
    {
        Connection con = getConnection();
        CallableStatement cs = con.prepareCall("{call myStorProc(?)}");
        cs.setString(1, id);
        ...
        //code to call resultset and retrieve SomeBean goes here
        ..
        return SomeBean;                
    }
}
public class MyTests extends TestCase {
    public testGetcontents ()
    {
        MyDAO myd = new MyDAO ();
        SomeBean smb = myd.getContents("someparm");
        assertEquals (5, smb.getSomeVal());
    }
}

我应该在我的测试用例中做些额外的工作.. 。?如果是这样的话?

Should I be doing something extra in my testcase...? if so what?

编辑:

我得到的错误是:

java.lang.NoClassDefFoundError: com/iplanet/ias/admin/common/ASException
        at java.lang.ClassLoader.defineClass1(Native Method)


推荐答案

你的DAO有一个硬连线的JNDI查找字符串。除非您有可用的JNDI查找服务,否则它将无法建立连接。

Your DAO has a JNDI lookup string hard wired into it. Unless you have a JNDI lookup service available, it won't be able to get a connection.

我不认为DAO应该负责获取数据库连接。此设计不允许您为工作单元设置事务,因为DAO无法知道它是否是更大工作单元的一部分。

I don't think a DAO should be responsible for acquiring a database connection. This design won't allow you to set transactions for a unit of work, because a DAO can't know if it's part of a larger unit of work.

我' d建议将连接传递给DAO,也许将其传递给它的构造函数。这样,如果单个工作单元中有多个DAO,则服务层可以建立适当的事务边界。

I'd recommend passing the connection into the DAO, perhaps into its constructor. That way a service layer can establish appropriate transaction boundaries if there's more than one DAO in a single unit of work.

此设计将具有使其成为可能的额外好处您的应用程序是否适当地使用其JNDI资源以及您的测试从DriverManager获取其连接,而无需使用JNDI查找。您有两个不同的来源来获取数据源或连接 - 一个用于应用程序,另一个用于测试。

This design will have the added benefit of making it possible for your application to use its JNDI resource appropriately and your test to get its connection from a DriverManager, without having to use a JNDI lookup. You have two different sources for acquiring the DataSource or Connection - one for the app and another for the test.

更新:

这就是我的意思,用你的代码表示:

Here's what I mean, expressed in your code:

public class DBConnectionManager 
{
    public static final String DB_URL = "jdbc/RSRC/my/connection/mydb"

    public Connection getConnection (String jndiLookup)
    {
        DataSource ds = ServiceLocator.getInstance().getDataSource(jndiLookup);

        return ds.getconnection();
    } 

    public Connection getConnection(String driver, String url, String username, String password)
        throws ClassNotFoundException, SQLException
    {
        Class.forName(driver);

        return DriverManager.getConnection(url, username, password);
    }
}

public class MyDAO 
{
    private Connection connection;

    public MyDao(Connection connection)
    {
        this.connection = connection;
    }

    public SomeBean getContents (String id)
    {
        CallableStatement cs = this.connection.prepareCall("{call myStorProc(?)}");
        this.connection.setString(1, id);

        //code to call resultset and retrieve SomeBean goes here

        return someBean;                
    }
}

您没有显示正确关闭资源或交易。从这个代码判断,你将在两个方面遇到麻烦。我会仔细考虑你的实现。

You show nothing about closing resources properly or transactions. Judging by this code, you'll be in trouble on both counts. I'd think carefully about your implementation.

我会向你推荐Spring JDBC。您可以在Spring中编写DAO而无需重写整个应用程序。

I'll recommend Spring JDBC to you. You can write your DAOs in Spring without rewriting your whole app.

我还要指出您可能也在查看泛型:不要重复DAO

I'll also point out that you might also be looking at generics: Don't Repeat The DAO.

这篇关于在J2ee应用程序上为DAO编写测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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