JUnit使用@Theory进行不同数据XML文件的测试

huangapple go评论79阅读模式
英文:

JUnit testing with different data XML files using @Theory

问题

我在使用多个数据文件时,对于在JUnit测试中使用@Theory遇到了困难。
我正试图将两个XML文件用作我的测试输入,因此我有以下代码:

public class XmlParserTest
{
    @DataPoint
    public static Document document;
    @DataPoint
    public static Document nsDocument;

    @Before
    public void before() throws Exception
    {
        InputStream is = getClass().getResourceAsStream("xmlTest.xml");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();

        XmlParserTest.document = builder.parse(is);
        XmlParserTest.document.getDocumentElement().normalize();

        is.close();
        is = getClass().getResourceAsStream("xmlNSTest.xml");

        XmlParserTest.nsDocument = builder.parse(is);
        XmlParserTest.nsDocument.getDocumentElement().normalize();
    }

    @Theory
    public void testGetAttribute(Document doc) throws Exception
    {
        NodeList ln = doc.getElementsByTagNameNS("*", "Event");
        ...
    }
}

基本上,我希望使用我的两个加载的XML文件运行该测试。
但是我收到异常:java.lang.Exception: No runnable methods

我查看了参数化字段,并看过了@Theory与静态设置字段的简单示例,但我无法弄清楚如何加载和使用文件。

任何见解都将非常有帮助。

英文:

I am having difficulty finding a solution to using @Theory on a JUnit test when using multiple data files.
I am trying to use 2 XML files as input to my tests so I have the following:

public class XmlParserTest
{
    @DataPoint
    public static Document document;
    @DataPoint
    public static Document nsDocument;

    @Before
    public void before() throws Exception
    {
        InputStream is = getClass().getResourceAsStream("xmlTest.xml");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();

        XmlParserTest.document = builder.parse(is);
        XmlParserTest.document.getDocumentElement().normalize();

        is.close();
        is = getClass().getResourceAsStream("xmlNSTest.xml");

        XmlParserTest.nsDocument = builder.parse(is);
        XmlParserTest.nsDocument.getDocumentElement().normalize();
    }

    @Theory
    public void testGetAttribute(Document doc) throws Exception
    {
        NodeList ln = doc.getElementsByTagNameNS("*", "Event");
        ...
    }
}

So basically I want to run that test with my two loaded XML files.
I get the Exception: java.lang.Exception: No runnable methods

I've looked at parameterized fields and have seen simple examples of @Theory with statically set fields but I can't really figure out how to load and use files.

Any insight would be great.

答案1

得分: 1

以下是您要翻译的内容:

理论测试需要一个特殊的运行器(请参阅文档):

@RunWith(Theories.class)
public class XmlParserTest {
  ....
}

否则,JUnit 将尝试将该类作为“普通”单元测试运行,因此它会寻找带有 @Test 注解的方法。显然,没有这样的方法,因此会出现异常。

英文:

Theories require a special runner (see documentation):

@RunWith(Theories.class)
public class XmlParserTest {
  ....
}

Otherwise, JUnit tries to run the class as a "regular" unit test, so it looks for methods annotated with @Test. Obviously there are no methods like this, hence the exception.

huangapple
  • 本文由 发表于 2020年8月26日 03:57:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63586232.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定