2014-10-27 71 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); 
    } 

} 

}

就是这样... :)