'How to fix sonar issue "Disable access to external entities in XML parsing" in Junits

from what i have understood that in order to get rid of the sonar issue "Disable access to external entities in XML parsing" , we need to have the following code in place

TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");

But when i do this, my JUnits which invokes this code fails saying

Failed to read schema document 'myschema.xsd', because 'file' access is not allowed due to restriction set by the accessExternalSchema property.

So how can we get around this? If we fix sonar issue, our Junit fails .

I also tried inistiasing the factory like below, but no change in error

TransformerFactory transformerFactory = new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl();


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source