mercredi 21 novembre 2018

Disable a part of Java Program during JUnit testing

I am testing my Java program with JUnit. The program includes some JavaFX GUI interface and other logging code. However, during testing, I don't want these to be tested. Is there any way to switch the code between testing and development?

The description can be abstract, I'll use an example:

public class Helloworld {

/**
 * @param args the command line arguments
 */
public static int greetingCnt = 0;

public static void main(String[] args) {
    Helloworld helloword = new Helloworld();
    helloword.greetWorld();
    helloword.greetWorld();
    helloword.greetWorld();
    System.out.println("Greating count: " + greetingCnt);
}

public void greetWorld() {
    System.out.println("Hello World!");
    ++greetingCnt;
    //some other computation...
}

}

In this example, if I only want to test the correct number of greetingCnt but not want to have anything printed or any extra computation to be performed. But during actual program execution, there is no influence on the function of the program. May I know if there is any way to do it?

Thank you!

Aucun commentaire:

Enregistrer un commentaire