0

我写了一个调用常量类的 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 线程访问这个列表时,我遇到了这个错误。

    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那么它工作正常。知道为什么会这样吗?

4

1 回答 1

0

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

导入 java.util.ArrayList;导入 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); 
} 

}
于 2012-07-27T08:49:30.367 回答