Wednesday 25 January 2017

unit testing - How to write a JUnit test in Java, that checks one particular print that depends on a userinput in a loop

I try to write JUnit tests in IntelliJ for an application, that modifies a String. Now how can I test one particular print on the console?




Let's assume the start-string is stored in args[0] as "Hello World" and we have a method that can change a single letter in the string. The string will be printed out after the execution of the method.



public class modifyString {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

while(true) {
// Print the string

System.out.println(args[0]);

// Wait for a userinput that only contains two chars,
// e.g. '0h' <- 'Change the index 0 to 'h''
// (the order of the chars is unimportant)
String input = scanner.nextLine();

if(preconditionFullfilled(input)) {
executeOperation(input, args);
}

}
}
}


For example: The expected output for the input '0h' with a 'Hello World' string in args[0] should be 'hello World'.



How do I write a JUnit test to test this particular result?

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...