0

I want to write JUNIT tests for this function

public static int creerFichierXml(TextField titre_projet,TextField description1,TextField svn,TextField planning1,TextField planning2,TextField goals,TextField mail){
//Créer le fichier XML et l'envoyer au serveur

    int returnCode = 0;
    ParseToXML j=new ParseToXML();
    try {
j.main(description1, svn, planning1, planning2, goals,mail);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }

HttpClient client = new HttpClient(); 
PostMethod postMethod = new PostMethod("http://localhost:8080/createItem?name="+titre_projet.toString()); 
postMethod.setRequestHeader("Content-type", "application/xml; charset=ISO-8859-1"); 
try {
    postMethod.setRequestBody(new FileInputStream(new File("C:/integrationContinue/src/main/resources/config.xml")));
    returnCode = client.executeMethod(postMethod);
    System.out.println("*********************************************"+returnCode);


} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (HttpException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return returnCode;
}

This code is used to create an xml file then send it to server with postMethod

Junit must test http Connection, Parse to Xml and call main function

skaffman
  • 398,947
  • 96
  • 818
  • 769
Jhon
  • 87
  • 4
  • 13

1 Answers1

0

If you are calling ParseToXML, HttpClient, PostMethod you are not doing unit testing because you are testing code outside the class under test. To truly unit test this, you would need to provide factory classes for ParseToXml, HttpClient and PostMethod. Then you would inject mock factories that would returns mocks for each of these classes. Then you would test that the mocks were invoked properly.

If you are testing that this code actually sent an HTTP Post method to server X, you are doing an integration test not a unit test.

John B
  • 32,493
  • 6
  • 77
  • 98