Java 使用@Theory使用不同的数据XML文件进行JUnit测试
在使用多个数据文件时,我很难找到在JUnit测试中使用@Theory的解决方案。 我尝试使用2个XML文件作为测试的输入,因此我有以下内容:Java 使用@Theory使用不同的数据XML文件进行JUnit测试,java,unit-testing,junit,Java,Unit Testing,Junit,在使用多个数据文件时,我很难找到在JUnit测试中使用@Theory的解决方案。 我尝试使用2个XML文件作为测试的输入,因此我有以下内容: public class XmlParserTest { @DataPoint public static Document document; @DataPoint public static Document nsDocument; @Before public void before() throws
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:没有可运行的方法
我看过参数化字段,也看过带有静态设置字段的@Theory的简单示例,但我真的不知道如何加载和使用文件
任何洞察都会很棒。理论需要一个特殊的跑步者(请参阅): 否则,JUnit会尝试以“常规”单元测试的形式运行该类,因此它会查找带有
@test
注释的方法。显然,没有这样的方法,因此出现了例外
@RunWith(Theories.class)
public class XmlParserTest {
....
}