简体   繁体   English

如何在junit中创建动态测试用例?

[英]How to create Dynamic test case in junit?

I have List<Map> and I want to write a dynamic test case based on the Lists value. 我有List<Map>并且我想基于Lists值编写一个动态测试用例。

My business logic as- 我的业务逻辑是-

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

My Test class- 我的测试课程-

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

}
}

Now I need three different test cases as List containing three maps. 现在,我需要三个不同的测试用例作为包含三个映射的List。 Is it possible? 可能吗? Kindly help me on this. 请帮助我。 Currently I am writing test method as "testResultsForListOfMap" But I want Three different test case for this. 目前,我正在将测试方法编写为“ testResultsForListOfMap”,但是我想要三个不同的测试用例。

Thanks Praveen 感谢Praveen

This class use to create test suite and add test cases into them 此类用于创建测试套件并将测试用例添加到其中

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

}

} }

That's it ... :) 而已 ... :)

如果listofMap方法采用三个参数map1,map2,map3,则可能的,否则无法进行模拟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM