This question already has an answer here:
I have a homework on testing and I am asked to implement a test class for the following code, which was given to us from our teacher:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
static double value;
static double output;
static double multiplyWith;
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a value between 2 and 11");
try {
value = Double.parseDouble(in.readLine());
} catch (NumberFormatException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
if(value<2 || value > 11) {
System.out.println("Wrong input, please restart");
System.exit(-1);
}
System.out.println("With which positive digit would you like to multiply your input in case it can be divided by 2 ?");
try {
String s = in.readLine();
multiplyWith = Integer.valueOf(s).intValue();
} catch (IOException e) {
e.printStackTrace();
}
//check if value is positive
if (multiplyWith <= 0) {
System.out.println("Please restart and input a positive number");
System.exit (-1);
}
// compute output
if (value % 2 == 0.0) {
output = value*multiplyWith; }
else {
output = value;
}
System.out.println ("Your result is " + output);
}
}
My question is how can I test the above code since it is dependent on the input stream and not on parameters and the output is always printed on runtime? I do not understand how I should test something like this. I would apreciate an explanation not just code, as I want to understand how this works. Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire