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

ost*_*ond 5 java xml junit exception

我将从字符串创建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);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我必须测试是否可以在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);
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能把这个测试扔掉ParserConfigurationException

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

tro*_*oig 3

ParserConfigurationException您的测试未通过,因为您在方法中精确捕获,因此它永远不会被抛出。要通过测试:

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

public String createCompleteExportXml(String xmlFilename, String content) throws ParserConfigurationException {
Run Code Online (Sandbox Code Playgroud)

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

  try {
     //...
  } catch (ParserConfigurationException pce) {
     LOGGER.trace("parsing error ", pce);
     throw pce;
  }
Run Code Online (Sandbox Code Playgroud)

希望对您有帮助

[更新]

如果你想模拟一个,你可以使用像模拟和模拟调用方法时抛出的ParserConfigurationException框架。Mockito / PowerMockDocumentBuilderFactoryParserConfigurationExceptionnewDocumentBuilder()

例子:

@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);
   }
Run Code Online (Sandbox Code Playgroud)

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

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>
Run Code Online (Sandbox Code Playgroud)

希望这就是您正在寻找的。

您可以找到MockitoPowerMock的更多文档

  • 谢谢。我将根据您的建议修改我的代码。但实际上我问的是如何模拟 ParserConfigurationException 错误。如何使 documentFactory.newDocumentBuilder() 无法工作,因为“无法创建满足所请求的配置的 DocumentBuilder。”?配置在哪里?我怎样才能改变它? (2认同)