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

时间:2014-10-27 11:08:50

标签: java junit

我有List<Map>,我想根据列表值编写动态测试用例。

我的业务逻辑为 -

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

}
}

现在我需要三个不同的测试用例作为包含三个地图的List。可能吗?请帮助我。 目前我正在编写测试方法为&#34; testResultsForListOfMap&#34;但我想要三种不同的测试用例。

由于 普利文

2 个答案:

答案 0 :(得分: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);
    }

}

}

就是这样......:)

答案 1 :(得分:-1)

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