I am new to Junit framework. I am trying to invoke private method in another class from Junit test class using reflection API. I am getting below error while running JUnit test:
java.lang.ExceptionInInitializerError
at com.test.eb.X.XConnection.dbOpen(XConnection.java:32)
at com.test.eb.X.admin.XRefTablePersister.getDbConnect(XRefTablePersister.java:33)
at com.test.eb.persistence.Persister.getSortedList(Persister.java:485)
at com.test.eb.X.entity.SearchPlan.<init>(SearchPlan.java:49)
at com.test.eb.X.entity.Tests.SearchPlanTest.testSearchPlan(SearchPlanTest.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:255)
at junit.framework.TestSuite.run(TestSuite.java:250)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.util.MissingResourceException: Can't find bundle for base name X_bootstrap, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1427)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1250)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:705)
at com.test.eb.X.XProperties.<clinit>(XProperties.java:12)
... 24 more
When I am debugging the application I do see the following:
the JAR of this class file belongs to container 'Junit 4' which does not allow modifications to source attachements on its entries.
Actual class
public class SP {
private java.lang.String searchText;
private java.lang.String searchOption;
private Hashtable searchResult;
private Persistable[] plnCategoryList;
private plnCategory persistplnCategory;
//CSS, for displaying the result
private String cssText;
private String cssTableHeader;
private String cssTable;
private boolean allowDelete;
public SP() {
super();
persistplnCategory =new plnCategory();
searchText= "";
searchOption = "";
RefTablePersister persister = new XRefTablePersister();
setAllowDelete(false);
//Default CSS values
cssText="mainbody";
cssTableHeader = "bodytableheader";
cssTable="bodytable";
try {
persistplnCategory.setplnCatgCd("");
Class persClass = Class.forName("com.org.plnCategory");
plnCategoryList = persister.getSortedList( persClass, new PersistableDescriptionComparator());
} catch (Exception e) {
e.printStackTrace();
}
}
private int searchpln() throws XException {
XConnection XConn = new XConnection();
searchResult = new Hashtable();
pln plnItem = new pln(), plnItem2;
cont contItem;
CoverageCode covcdItem;
Vector tempList;
Hashtable tempTable;
StringBuffer sqlContr = new StringBuffer("");
StringBuffer sqlCovCd = new StringBuffer("");
ResultSet rsContr;
ResultSet rsCovCd;
String plnCat="";
if (! persistplnCategory.getplnCatgCd().equals("") && ! persistplnCategory.getplnCatgCd().equals("0")){
plnCat = persistplnCategory.getplnCatgCd();
}
sqlContr.append("SELECT ")
.append(" p.pln_cd, ")
.append(" p.pln_version_nbr, ")
.append(" pcr.pln_catg_desc, ")
.append(" p.pln_eff_dt, ")
.append(" p.pln_end_dt, ")
.append(" cp.cont_nbr ");
sqlContr.append(" FROM ")
.append(" pln p, ")
.append(" pln_category_ref pcr, ")
.append(" cont_pln cp ");
sqlContr.append(" WHERE ")
.append(" p.pln_cd = " + getSearchText().trim())
.append(" AND p.pln_catg_cd = pcr.pln_catg_cd ")
.append(" AND p.pln_cd = cp.pln_cd ")
.append(" AND p.pln_version_nbr = cp.pln_version_nbr ");
if (!plnCat.equals("")){
sqlContr.append(" AND p.pln_catg_cd = '" + plnCat + "' ");
}
sqlContr.append(" ORDER BY ")
.append(" p.pln_cd, ")
.append(" p.pln_version_nbr, ")
.append(" cp.cont_nbr ");
try {
XConn.dbOpen();
rsContr = XConn.doQuery(sqlContr.toString());
tempTable = new Hashtable();
if (rsContr.next()){
plnItem = new pln();
plnItem.setNumber(rsContr.getString(1).trim());
plnItem.setVersion(rsContr.getString(2).trim());
plnItem.setplnCategoryText(rsContr.getString(3).trim());
plnItem.setEffectiveDate(rsContr.getDate(4));
plnItem.setEndDate(rsContr.getDate(5));
contItem = new cont();
contItem.setNumber(rsContr.getString(6).trim());
tempTable.put(contItem.toString(), contItem);
while(rsContr.next()){
plnItem2 = new pln();
plnItem2.setNumber(rsContr.getString(1).trim());
plnItem2.setVersion(rsContr.getString(2).trim());
//just add cont if it is still the same
if (plnItem.equals(plnItem2)) {
contItem = new cont();
contItem.setNumber(rsContr.getString(6).trim());
tempTable.put(contItem.toString(), contItem);
} else {
//save the pln info
plnItem.setconts(tempTable);
searchResult.put(plnItem.toString(), plnItem);
//create the pln
plnItem = new pln();
plnItem.setNumber(rsContr.getString(1).trim());
plnItem.setVersion(rsContr.getString(2).trim());
plnItem.setplnCategoryText(rsContr.getString(3).trim());
plnItem.setEffectiveDate(rsContr.getDate(4));
plnItem.setEndDate(rsContr.getDate(5));
tempTable = new Hashtable();
contItem = new cont();
contItem.setNumber(rsContr.getString(6).trim());
tempTable.put(contItem.toString(), contItem);
}
}
//save the last pln
plnItem.setconts(tempTable);
searchResult.put(plnItem.toString(), plnItem);
}
plnItem2 = (pln) searchResult.get(plnItem.toString());
if (plnItem2 != null){
plnItem2.setCoverageCodes(tempList);
searchResult.put(plnItem2.toString(), plnItem2);
}
}
}
return searchResult.size();}
Junit Class
public class SPTest extends TestCase{
public java.lang.String searchText;
public java.lang.String searchOption;
@Before
public void setUp() throws Exception {
searchText = "963 - 1";
searchOption = "pln";
}
@Test
public void testSearchpln() {
try {
Searchpln searchpln = new Searchpln();
Method method = Searchpln.class.getDeclaredMethod("searchpln", null);
method.setAccessible(true);
int plnresults = (Integer) method.invoke(searchpln, null);
assertNotNull(plnresults);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} finally{
//pbdbConn.dbClose();
}
}
}
When I am trying to invoke the searchpln() method it is failed to initialize database connection and hence I am getting java.lang.ExceptionInInitializerError.
The issue is now fixed. I have changed code by setting dbDriver,dbURL,dbUser & dbPassword details in class.