java - 如何在通过 DocumentBuidlerFactory 创建新的 xml 文件时生成 ParserConfigurationException

标签 java xml exception junit

我要从一个字符串创建一个 XML。看起来像

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public Document createCompleteExportXml(String xmlFilename, String content) {
    try {
        DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();

        //create the XML file here
    } catch (ParserConfigurationException pce) {
        LOGGER.trace("parsing error ", pce);
    }
}

现在我必须测试是否可以在 Junit 测试中捕获异常。

@Test(expected=ParserConfigurationException.class)
public void createCompleteExportXmlWithParseConfigurationException() {
    String xmlFilename = "junitExportTestWithParseConfigurationException.xml";
    String content = "any content";
    XmlFileWriter writer = new XmlFileWriter();
    Document doc = writer.createCompleteExportXml(xmlFilename, content);
}

如何让这个测试抛出 ParserConfigurationException

我让我的问题更具体:如何使 documentFactory.newDocumentBuilder() 无法工作,因为“无法创建满足请求配置的 DocumentBuilder。”?配置在哪里?如何故意将其更改为错误的?

最佳答案

您的测试未通过,因为您准确地捕捉到了 ParserConfigurationException在你的方法中,所以它永远不会被抛出。要通过测试:

1) 更改方法的签名(抛出异常)

public String createCompleteExportXml(String xmlFilename, String content) throws ParserConfigurationException {

2) 抛出 ParserConfigurationException .为此,您可以删除 catch block 或在 LOGGER.trace 之后抛出异常。 .第二个选项的示例:

  try {
     //...
  } catch (ParserConfigurationException pce) {
     LOGGER.trace("parsing error ", pce);
     throw pce;
  }

希望对你有帮助

[更新]

如果你想模拟一个ParserConfigurationException , 你可以使用像 Mockito / PowerMock 这样的框架模拟 DocumentBuilderFactory并模拟ParserConfigurationException方法 newDocumentBuilder() 时抛出被称为。

例子:

@RunWith(PowerMockRunner.class)
@PrepareForTest(DocumentBuilderFactory.class)
public class XmlFileWriterTest {

   @Test(expected = ParserConfigurationException.class)
   public void createCompleteExportXmlWithParseConfigurationException() throws Exception {
      String xmlFilename = "junitExportTestWithParseConfigurationException.xml";
      String content = "any content";
      XmlFileWriter writer = new XmlFileWriter();

      // Mock DocumentBuilderFactory: When method newDocumentBuilder() is called, throws a simulated ParserConfigurationException
      DocumentBuilderFactory mockDocumentBuilderFactory = PowerMockito.mock(DocumentBuilderFactory.class);
      PowerMockito.when(mockDocumentBuilderFactory.newDocumentBuilder()).thenThrow(new ParserConfigurationException("Simulated ex"));

      // Mock DocumentBuilderFactory.newInstance(), when is called, returns your mock instance mockDocumentBuilderFactory
      PowerMockito.mockStatic(DocumentBuilderFactory.class);
      PowerMockito.when(DocumentBuilderFactory.newInstance()).thenReturn(mockDocumentBuilderFactory);

      writer.createCompleteExportXml(xmlFilename, content);
   }

此测试通过(完成之前的代码建议)。

powerMock 的 Maven 依赖项:

<dependency>
     <groupId>org.powermock</groupId>
     <artifactId>powermock-module-junit4</artifactId>
     <version>1.5.4</version>
  </dependency>

  <dependency>
     <groupId>org.powermock</groupId>
     <artifactId>powermock-api-mockito</artifactId>
     <version>1.5.4</version>
  </dependency>

希望这就是您要找的。

您可以找到 Mockito 的更多文档和 PowerMock

关于java - 如何在通过 DocumentBuidlerFactory 创建新的 xml 文件时生成 ParserConfigurationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25645312/

相关文章:

java - 在 sbt 中强制执行 Scala 项目的 Java 版本?

java - 在 Java 中将字符串转换为 BigInteger 数组

xml - SVG CSS3 在悬停时从其他元素显示和隐藏元素

java - HtmlUnit - 将 HtmlPage 转换为 HTML 字符串?

Android如何调试DeadObjectException

java - com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException : Duplicate entry '' for key 'PRIMARY'

python - Python 中的 BaseException 子类问题

java - HTTP 未返回 304

java - 验证连接的数据库 - Java 持久性 API

c# - 忽略要 (XML) 序列化的属性