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

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

JUnit testing with different data XML files using @Theory

问题

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

  1. public class XmlParserTest
  2. {
  3. @DataPoint
  4. public static Document document;
  5. @DataPoint
  6. public static Document nsDocument;
  7. @Before
  8. public void before() throws Exception
  9. {
  10. InputStream is = getClass().getResourceAsStream("xmlTest.xml");
  11. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  12. factory.setNamespaceAware(true);
  13. DocumentBuilder builder = factory.newDocumentBuilder();
  14. XmlParserTest.document = builder.parse(is);
  15. XmlParserTest.document.getDocumentElement().normalize();
  16. is.close();
  17. is = getClass().getResourceAsStream("xmlNSTest.xml");
  18. XmlParserTest.nsDocument = builder.parse(is);
  19. XmlParserTest.nsDocument.getDocumentElement().normalize();
  20. }
  21. @Theory
  22. public void testGetAttribute(Document doc) throws Exception
  23. {
  24. NodeList ln = doc.getElementsByTagNameNS("*", "Event");
  25. ...
  26. }
  27. }

基本上,我希望使用我的两个加载的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:

  1. public class XmlParserTest
  2. {
  3. @DataPoint
  4. public static Document document;
  5. @DataPoint
  6. public static Document nsDocument;
  7. @Before
  8. public void before() throws Exception
  9. {
  10. InputStream is = getClass().getResourceAsStream("xmlTest.xml");
  11. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  12. factory.setNamespaceAware(true);
  13. DocumentBuilder builder = factory.newDocumentBuilder();
  14. XmlParserTest.document = builder.parse(is);
  15. XmlParserTest.document.getDocumentElement().normalize();
  16. is.close();
  17. is = getClass().getResourceAsStream("xmlNSTest.xml");
  18. XmlParserTest.nsDocument = builder.parse(is);
  19. XmlParserTest.nsDocument.getDocumentElement().normalize();
  20. }
  21. @Theory
  22. public void testGetAttribute(Document doc) throws Exception
  23. {
  24. NodeList ln = doc.getElementsByTagNameNS("*", "Event");
  25. ...
  26. }
  27. }

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

以下是您要翻译的内容:

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

  1. @RunWith(Theories.class)
  2. public class XmlParserTest {
  3. ....
  4. }

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

英文:

Theories require a special runner (see documentation):

  1. @RunWith(Theories.class)
  2. public class XmlParserTest {
  3. ....
  4. }

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:

确定