NastyDiaper
NastyDiaper

Reputation: 2558

JUnit testing with different data XML files using @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.

Upvotes: 0

Views: 209

Answers (1)

Mark Bramnik
Mark Bramnik

Reputation: 42521

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.

Upvotes: 1

Related Questions