带有静态最终List的JUnit出错

时间:2012-07-27 07:07:15

标签: java junit

我写了一个调用Constant类的JUnit,这里我们试图访问静态最终列表

public static final List<String> SKIPPED_DIMENSION_LIST = new ArrayList<String>();

static{ SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_DISPLAY); 
    SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_SUB); 
    SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_SUB_GROUP); 
    SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_SUB_GROUP_ID); 
    SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_ID); 
    SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_GROUP_ID); }

现在,在通过JUnit线程访问此List时,我遇到了这个错误。

    java.lang.ExceptionInInitializerError
        at java.lang.J9VMInternals.initialize(J9VMInternals.java:222)
        at com.mns.commerce.searchnav.constants.MSEndecaConstants.<clinit>(MSEndecaConstants.java:169)
        at java.lang.J9VMInternals.initializeImpl(Native Method)
        at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
        at com.mns.commerce.searchnav.utils.MSSearchResposeBOTransformer.getRefinementsDetails(MSSearchResposeBOTransformer.java:460)
        at com.mns.commerce.searchnav.utils.MSSearchResposeBOTransformer.buildFacets(MSSearchResposeBOTransformer.java:362)
        at com.mns.commerce.searchnav.utils.MSSearchResposeBOTransformer.populateResults(MSSearchResposeBOTransformer.java:106)
        at com.mns.commerce.searchnavunit.test.MSSearchResposeBOTransformerTest.testPopulateResults(MSSearchResposeBOTransformerTest.java:97)

然而,如果定义静态Enum而不是static final list,那么它工作正常。知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

此代码也在运行,我对此代码没有任何错误。我的JDK版本是1.6。

import java.util.ArrayList; import java.util.List;

public class test {

private static final String PRODUCT_CATEGORY_DISPLAY = "a";
private static final String PRODUCT_CATEGORY_SUB = "b";
private static final String PRODUCT_CATEGORY_SUB_GROUP = "c";
private static final String PRODUCT_CATEGORY_SUB_GROUP_ID = "d";
private static final String PRODUCT_CATEGORY_ID = "e";
private static final String PRODUCT_CATEGORY_GROUP_ID = "f";
public static final List<String> SKIPPED_DIMENSION_LIST = new ArrayList<String>();

static{          
    SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_DISPLAY);
    SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_SUB);
    SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_SUB_GROUP);
    SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_SUB_GROUP_ID);
    SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_ID);
    SKIPPED_DIMENSION_LIST.add(PRODUCT_CATEGORY_GROUP_ID);
}      /**      * @param args      */     
public static void main(String[] args) {   
    System.out.println(SKIPPED_DIMENSION_LIST); 
} 

}