Java Junit4测试实现,使用从XML传递的参数(属性值+;节点值)?

Java Junit4测试实现,使用从XML传递的参数(属性值+;节点值)?,java,xml,junit4,parameterized,Java,Xml,Junit4,Parameterized,我知道我可以硬编码它(解析xml、提取),但是有没有一种方法可以像Feed4TestNG那样提供(属性值+节点值)(它目前只支持csv和excel文件) 我是Java新手,任何专家的见解都会有所帮助。谢谢 参数@Parameters的主体不仅限于数据,您可以在此方法中使用任何喜欢的java代码,包括引发异常: @Parameters public static Collection<Object[]> data() throws IOException { List<O

我知道我可以硬编码它(解析xml、提取),但是有没有一种方法可以像Feed4TestNG那样提供(属性值+节点值)(它目前只支持csv和excel文件)


我是Java新手,任何专家的见解都会有所帮助。谢谢

参数
@Parameters
的主体不仅限于数据,您可以在此方法中使用任何喜欢的java代码,包括引发异常:

@Parameters
public static Collection<Object[]> data() throws IOException {
    List<Object[]> data = new ArrayList<>();
    // replace getClass() with <nameofclass>.class
    try(InputStream in = this.getClass().getResourceAsStream()) { 
        //parse body here
        data.add(new Object[]{attribute, value});
    }
    return data;
}
@参数
公共静态集合数据()引发IOException{
列表数据=新的ArrayList();
//将getClass()替换为.class
尝试(InputStream in=this.getClass().getResourceAsStream()){
//在这里解析主体
add(新对象[]{attribute,value});
}
返回数据;
}

根据您使用的XML框架,您需要解析XML节点,并将其放入要返回的列表中。

因此,我在这里要做的就是:

如果您认为我可以改进我的代码,请提交您的更正。

@RunWith(参数化的.class) 公共类DataDrivenTests{

private String c;
private String b;
private static Collection<Object[]> a;
@Parameters
public static Collection<Object[]> xmlData() throws IOException{

        File file = new File("xmlfile.xml");
        InputStream xml1 = new FileInputStream(file);
        return  new xmlData(xml1).getData();

}



public DataDrivenTests(String c, String b) {
    super();
    this.c = c;
    this.b = b;


}


@Test
public void shouldCalculateATimesB() {
    boolean assertion = false;

    if(c.equals(Parser.parse("Parse this string to Attribute and Value"))){
        assertion = true;

    }

    assertTrue(assertion);


}
私有字符串c;
私有字符串b;
私有静态集合a;
@参数
公共静态集合xmlData()引发IOException{
File File=新文件(“xmlfile.xml”);
InputStream xml1=新文件InputStream(文件);
返回新的xmlData(xml1.getData();
}
公共数据驱动测试(字符串c、字符串b){
超级();
这个.c=c;
这个.b=b;
}
@试验
public void应计算timesb(){
布尔断言=false;
if(c.equals(Parser.parse(“将此字符串解析为属性和值”)){
断言=真;
}
assertTrue(断言);
}
}

xmlData.java

公共类xmlData{

private transient Collection<Object[]> data = null;

public xmlData(final InputStream xml)throws IOException{

    this.data = loadFromXml(xml);

}

public Collection<Object[]> getData(){

    return data;
}



private Collection<Object[]> loadFromXml(final InputStream xml)
        throws IOException {
List <Object[]> ism_code_map = new ArrayList<Object[]>();

    try{
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);
    DocumentBuilder dBuilder;

    dBuilder = dbFactory.newDocumentBuilder();

    Document doc = dBuilder.parse(xml);
    doc.getDocumentElement().normalize();

    XPath xPath = XPathFactory.newInstance().newXPath();

    XPathExpression expression = xPath.compile("//e");


    NodeList nodes = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);

    for (int i =0; i< nodes.getLength(); i++){

        Node nNode = nodes.item(i);
        //System.out.println("\nCurrent Element:" + nNode.getTextContent());

                if (nNode.getNodeType() == Node.ELEMENT_NODE){
                    Element eElement = (Element) nNode;


                    if(eElement.getAttribute("attrname") != null && !eElement.getAttribute("attrname").isEmpty()){

                        code_map.add(new Object[]{"attrname",eElement.getAttribute("attrname")});

                    }


    }catch(ParserConfigurationException e){
        e.printStackTrace();

    }catch(SAXException e){
        e.printStackTrace();

    }catch(IOException e){
        e.printStackTrace();
    }catch(XPathExpressionException e){
        e.printStackTrace();
    }
    catch(NullPointerException e){
        e.printStackTrace();

    }


return code_map;

}
private transient Collection data=null;
公共xmlData(最终输入流xml)引发IOException{
this.data=loadFromXml(xml);
}
公共集合getData(){
返回数据;
}
私有集合loadFromXml(最终输入流xml)
抛出IOException{
List ism_code_map=new ArrayList();
试一试{
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware(true);
文档生成器dBuilder;
dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(xml);
doc.getDocumentElement().normalize();
XPath=XPathFactory.newInstance().newXPath();
XPathExpression表达式=xPath.compile(“//e”);
NodeList nodes=(NodeList)expression.evaluate(doc,XPathConstants.NODESET);
对于(int i=0;i

}

谢谢你的回复!我不能在静态中使用它,但我明白了主要思想。我希望做一些类似的事情,在测试类的
@Parameters
中也使用静态?你是说示例对吗?我在这里有点困惑。