使用JUnit测试ServiceLocator [英] Testing ServiceLocator using JUnit

查看:89
本文介绍了使用JUnit测试ServiceLocator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的>上一个问题的后续问题问题.

我正在尝试为我的ServiceLocator类编写测试用例,但它给了我以下错误:

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

我的测试用例:

public void testServiceLocator () throws UivException, NamingException
{
    DataSource ds = ServiceLocator.getInstance().getDataSource("jdbc/RSRC/my/mydb");
    //I have not put any assert statements because i know above line is failing
}

上面的代码在如下所示的getInstance()方法上失败:

static public ServiceLocator getInstance() throws UivException {
    try {
        if (me == null) {
          synchronized(ServiceLocator.class) {
            me = new ServiceLocator();
          }
        }
        return me;
    }
    catch (UivException e) {
          throw new UivException(ErrorCode.SERVICE_LOCATOR_ERROR,
                                 ErrorCode.SERVICE_LOCATOR_LOOKUP_ERROR,
                                 e.getMessage());
    }
}

我知道此ServiceLocator可以正常工作,因为当我从前端测​​试应用程序时,没有任何问题.我编写此测试用例的唯一原因是因为我要测试我的 DAO .而对于我来说,测试我的DAO ServiceLocator必须起作用(来自 JUnit ).

我不知道如何处理错误消息.有没有人想建议我可以尝试的东西,希望能成功?

ServiceLocator构造函数

private ServiceLocator() throws UivException  {
    try {
        ic = new InitialContext();
        // System.out.println("Created the Initial Context");
        cache = Collections.synchronizedMap(new HashMap());
    }
    catch (NamingException ne) {
        throw new UivException(ErrorCode.SERVICE_LOCATOR_ERROR,
                               0, ne.getMessage());
    }
    catch (NullPointerException e) {
          throw new UivException(ErrorCode.SERVICE_LOCATOR_ERROR,
                                 0, e.getMessage());
    }
}

解决方案

那么,调用测试时,com.iplanet.ias.admin.common.ASException在类路径上吗?

您是依靠应用服务器在其类路径中包含JDBC驱动程序的库,还是您自己进行部署?

This is a follow up question to my previous question.

I am trying to write test case for my ServiceLocator class but it gives me the following error:

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

My test case:

public void testServiceLocator () throws UivException, NamingException
{
    DataSource ds = ServiceLocator.getInstance().getDataSource("jdbc/RSRC/my/mydb");
    //I have not put any assert statements because i know above line is failing
}

The above code fails on getInstance() method which looks like this:

static public ServiceLocator getInstance() throws UivException {
    try {
        if (me == null) {
          synchronized(ServiceLocator.class) {
            me = new ServiceLocator();
          }
        }
        return me;
    }
    catch (UivException e) {
          throw new UivException(ErrorCode.SERVICE_LOCATOR_ERROR,
                                 ErrorCode.SERVICE_LOCATOR_LOOKUP_ERROR,
                                 e.getMessage());
    }
}

I know this ServiceLocator works fine because when I test my application from the front end there are no issues. Only reason I am writing this test case is because I want to test my DAO. And for me to test my DAO ServiceLocator has to work (from JUnit).

I don't have a clue what to make of the error message. Does anyone want to suggest something I can try that will hopefully work?

EDIT: ServiceLocator constructor

private ServiceLocator() throws UivException  {
    try {
        ic = new InitialContext();
        // System.out.println("Created the Initial Context");
        cache = Collections.synchronizedMap(new HashMap());
    }
    catch (NamingException ne) {
        throw new UivException(ErrorCode.SERVICE_LOCATOR_ERROR,
                               0, ne.getMessage());
    }
    catch (NullPointerException e) {
          throw new UivException(ErrorCode.SERVICE_LOCATOR_ERROR,
                                 0, e.getMessage());
    }
}

解决方案

Well, is com.iplanet.ias.admin.common.ASException on the classpath when you invoke your tests?

Are you relying on the app server to have the JDBC driver's libraries in it's classpath, or are you deploying it yourself?

这篇关于使用JUnit测试ServiceLocator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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