lundi 5 novembre 2018

How to test a void method using JUnit and/or Mockito

Apologies in advance - I know this has been asked a thousand times but I've looked through so many articles/documentation and I'm just so f****** lost.

I have a class that takes in an XML file and then uses DocumentBuilder to parse it into a new file that will be used as a source for other classes to use.

I need to test my method (which is void). My project is completed but I need to test.

If anyone could be so kind to show me how this would be done, I can go ahead and follow that same logic with my other classes, as 90% of the methods in my project do not return anything.

Thanks...

 public class XmlToCsv {


    public static void xmlToCsv(String sourceXlsFile, String sourceCsvFile, String sourceXmlFile) throws Exception {

        //define the files
        File stylesheet = new File(sourceXlsFile);
        File xmlSource = new File(sourceXmlFile);

        //create the DocumentBuilder to parse the XML file
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(xmlSource);

        //input the stylesheet to transform the XML to
        StreamSource stylesource = new StreamSource(stylesheet);
        Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);

        //write a new output file using the stylesheet format
        Source source = new DOMSource(document);
        Result outputTarget = new StreamResult(new File(sourceCsvFile));
        transformer.transform(source, outputTarget);

    }
}

Aucun commentaire:

Enregistrer un commentaire