2014-10-27 73 views
0

我有List<Map>,我想根據Lists值編寫一個動態測試用例。如何在junit中創建動態測試用例?

我的商業邏輯原樣

public List<Map<Object, Object>> listofMap() { 
    List<Map<Object, Object>> listofmapObj = new ArrayList<Map<Object, Object>>(); 
    Map<Object, Object> map1 = new HashMap<Object, Object>(); 
    map1.put("map1key", "map1value");  
    Map<Object, Object> map2 = new HashMap<Object, Object>(); 
    map2.put("map2key", "map2value"); 
    Map<Object, Object> map3 = new HashMap<Object, Object>(); 
    map3.put("map3key", "map3value"); 
    listofmapObj.add(map1); 
    listofmapObj.add(map2); 
    listofmapObj.add(map3); 
    return listofmapObj; 
} 

我的測試接收機類

public class TestCaseCreateListOfMap extends TestCase { 

StringBuffer strBuff = new StringBuffer(); 


String name; 

@Test 
public void testResultsForListOfMap(){ 


    try { 

     AddListOfMap obj=new AddListOfMap(); 

     List<Map<Object, Object>> listofmap= obj.listofMap(); 

     for (Map<Object, Object> map : listofmap) { 
      for (Map.Entry<Object, Object> entry : map.entrySet()) { 
       System.out.println(entry.getKey() + " - " + entry.getValue());     
       strBuff.append(entry.getKey() + " - " + entry.getValue()); 
      } 
     } 
    } 
    catch (Exception e) { 
     // TODO: handle exception 
     strBuff.append(e.getMessage()); 

    } 

    if(strBuff.length()>0){ 
     fail("" + strBuff); 
    } 

} 
} 

現在我需要三種不同的測試案例包含三個地圖列表。可能嗎?請幫助我。 目前我正在編寫測試方法爲「testResultsForListOfMap」,但我想要三個不同的測試用例。

感謝 普利文

回答

-1

它的可能的,如果listofMap方法取三個參數MAP1,MAP2,MAP3否則嘲諷是不可能的。

+0

瞭解決方案使用測試套件的概念..謝謝:) – 2014-10-28 09:58:25

+0

除了措辭不佳之外,這篇文章不會嘗試回答OP的題。 – jpaugh 2016-03-24 17:59:13

+0

@jpaugh可能是你正確的,但它有助於OP,這就是爲什麼我不刪除我的帖子。 – 2016-03-30 21:40:53

0

這個類使用來創建測試套件,並添加測試用例到他們

public class TestCaseDemo { 
public static Test suite() { 
    AddListOfMap obj = new AddListOfMap(); 
    List<Map<Object, Object>> listofmap = obj.listofMap(); 
    TestSuite suite = new TestSuite("ABC"); 
    suite.addTest(new JunitFileTestSuite(listofmap)); 
    return suite; 
} 

}

public class JunitFileTestSuite extends TestSuite{ 

protected List<Map<Object,Object>> map; 
public JunitFileTestSuite(List<Map<Object,Object>> map) { 

    this.map=map; 
    // recursively add cases 
    addTestCases(); 
} 
protected void addTestCases(){ 
    for (Map<Object, Object> listofmap : map) { 
     JunitFileTestCase jftc = new JunitFileTestCase(listofmap); 
      addTest(jftc); 

     } 
    } 

} 



public class JunitFileTestCase extends TestCase { 
protected Map<Object, Object> listofmap; 
public JunitFileTestCase(Map<Object, Object> listofmap) { 
    super(); 
    this.listofmap = listofmap; 
    for (Map.Entry<Object, Object> entry : listofmap.entrySet()) { 
     System.out.println(entry.getKey() + " - " + entry.getValue()); 
     setName(entry.getKey()); 
    } 
} 
protected void runTest() throws Throwable { 
    executeTestCase(listofmap); 
} 
// should not override 
final void executeTestCase(Map<Object, Object> fileName) throws Exception { 
    StringBuffer sbf = new StringBuffer(); 
    for (Map.Entry<Object, Object> entry : listofmap.entrySet()) { 
     System.out.println(entry.getKey() + " - " + entry.getValue()); 

     if((""+entry.getKey()).contains("Fail")){ 
      sbf.append(entry.getKey() + " - " + entry.getValue()); 
     } 
    } 
    if (sbf.length() > 0) { 
     fail("" + sbf); 
    } 

} 

}

就是這樣... :)