這是對我的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