Your test fails because you definitely catch a ParserConfigurationException in your method, so you never threw it. To pass the test:
1) change the signature of your method (throw exception)
public String createCompleteExportXml(String xmlFilename, String content) throws ParserConfigurationException {
2) Throw a ParserConfigurationException . To do this, you can remove the catch block or throw an exception after LOGGER.trace . An example for the second option:
try { //... } catch (ParserConfigurationException pce) { LOGGER.trace("parsing error ", pce); throw pce; }
Hope this helps you
[UPDATE]
If you want to simulate a ParserConfigurationException , you can use a framework like Mockito / PowerMock for the mock DocumentBuilderFactory and mimic that the ParserConfigurationException is ParserConfigurationException when the newDocumentBuilder() method is newDocumentBuilder() .
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();
This test pass (with previous code suggestions).
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 will be what you are looking for.
You can find additional documentation for Mockito and PowerMock.
troig source share