2015-06-18 40 views
0

我的問題圍繞着如何調用我已經存儲在主Junit測試類的另一個方法中的HashMap數據。從一個單獨的類/方法調用HashMap數據

@Test 
public void pciTest() throws Exception { 
    HashMap<String, String> examples = new HashMap<String, String>(); 

    examples.put(pciTestData()); 

    //Remaining testing code below 

    } 

//Storing Data Here 

public HashMap<String,String> pciTestData() { 
    HashMap<String, String> examples = new HashMap<String, String>(); 

    public static HashMap<String, String> getExamples() { 

     return examples; 
    } 

    examples.put("credit card", "4929028573388403\n" + 
      "4024007140713941\n" + 
      "4528684534391095\n" + 
      "5188872375900391\n" + 
      "5449835900541183\n" + 
      "5525878422452056\n"); 
      //more data below 
    } 

我想從pciTestData取數據(),並將其放置在HashMap中例子pciTest(),由examples.put(pciTestData())部分所看到。

當我測試各種方法時,getExamples()方法是一種佔位符,不確定是否需要這樣做。

我試過pciTestData()。get.keySet()但是返回一個錯誤。

+0

'pciTestData()get.keySet()'應該是'pciTestData()鍵設置()'對於初學者。但你可以做'examples.putAll(pciTestData())' – mstbaum

+0

請指定你在標籤中使用的語言。它有助於回答問題。 –

回答

0

只要做examples.putAll(pciTestData()),而不是examples.put(pciTestData())。 或者,因爲它似乎你不把任何東西的例子來自糾察隊正從pciTestData數據之前,你可以做examples = pciTestData().

getExamples()方法是沒有用的(這是很奇怪也爲你聲明內的另一個靜態方法方法如果我正確閱讀代碼)。

+0

謝謝,看着你的例子。 – cjawahar

0

你想達到什麼目的?

對於我的理解,我想你想複製你的pciTestData()方法返回的結果地圖在測試變量examples

如果你想在地圖的內容進行復制,您可以使用Map#putAll((Map<? extends K,? extends V> m)

examples.putAll(pciTestData()); 

如果你只是想從另一種方法(在這裏,測試方法)來訪問你的方法的結果,你只需要您的實例變量,就像任何其他一個:

examples = pciTestData(); 
相關問題