0

我是 Junit 框架的新手。我正在尝试使用反射 API 从 Junit 测试类调用另一个类中的私有方法。运行 JUnit 测试时出现以下错误:

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

当我调试应用程序时,我确实看到以下内容:

此类文件的 JAR 属于容器“Junit 4”,该容器不允许修改其条目上的源附件。

实际班级

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类

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();
    }
}

}

4

2 回答 2

1

看起来这与您的私有方法无关。异常在 SP 的构造函数中被抛出在线:

plnCategoryList = persister.getSortedList( persClass, new PersistableDescriptionComparator());
于 2016-01-28T06:45:34.663 回答
0

当我尝试调用 searchpln() 方法时,无法初始化数据库连接,因此我收到 java.lang.ExceptionInInitializerError。

该问题现已修复。我通过在类中设置 dbDriver、dbURL、dbUser 和 dbPassword 详细信息来更改代码。

于 2016-01-28T19:58:02.127 回答