这是对我的previous question的后续问题。使用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)。
我不知道如何处理错误消息。有没有人想提出一些我可以尝试的方法,希望能够奏效?
编辑:服务定位构造
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());
}
}
我依靠应用程序服务器。因为应用程序服务器已经拥有了所有我想编写测试并与之相处的设置。 – Omnipresent 2009-11-09 02:27:36
com.iplanet的东西似乎是Sun App Server的管理员属性。这可能需要从应用服务器获取JDBC资源。 ...但是应用服务器上未部署Junit测试... – Omnipresent 2009-11-09 02:31:08
但是如果您的单元测试调用尝试加载JDBC驱动程序的代码,那么在执行单元测试时,JDBC驱动程序需要位于类路径中。 – 2009-11-09 03:41:54