Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a ParserConfigurationException while creating a new xml file through DocumentBuidlerFactory

I am going to create a XML from a string. It looks like

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);
    }
}

Now I must test if the exception can be caught in a Junit test.

@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);
}

How can I make this test throw the ParserConfigurationException?

I make my question more concrete: How can I make documentFactory.newDocumentBuilder() not able to work, because "a DocumentBuilder cannot be created which satisfies the configuration requested."? Where is the configuration? How can I change it intentionally to a wrong one?

like image 382
ostmond Avatar asked Sep 03 '14 13:09

ostmond


1 Answers

Your test is not passing because you are catching precisely ParserConfigurationException in your method, so it's never thrown. To pass the test:

1) change the signature of your method (throwing exception)

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

2) Throw the ParserConfigurationException. To do this, you can remove the catch block or throw the exception after LOGGER.trace. Example for the second option:

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

Hope it helps you

[UPDATE]

If you want to simulate a ParserConfigurationException, you can use a framework like Mockito / PowerMock to mock DocumentBuilderFactory and simulate that ParserConfigurationException is thrown when method newDocumentBuilder() is called.

Example:

@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);
   }

This test pass (with previous code suggestions done).

Maven dependencies for powerMock:

<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>

Hope this would be what you are looking for.

You can find more documentation of Mockito and PowerMock

like image 146
troig Avatar answered Nov 01 '22 09:11

troig