2012-07-27 58 views
0

我寫一個JUnit在那裏調用恆類,在這裏我們試圖訪問一個靜態最終名單錯誤的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那麼它工作正常。任何想法爲什麼發生這種情況?

+0

如果從列表聲明中刪除最終修飾符會發生什麼? – JamesB 2012-07-27 07:39:58

+0

嘗試在靜態初始化器中的代碼周圍放置try/catch(Throwable t)塊,並查看它是否實際上拋出了某些描述的異常。這會給你一個更好的線索。 – 2012-07-27 08:52:41

+0

有關於此的任何更新? – Sikorski 2012-07-27 08:54:44

回答

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

}