Ваша проблема здесь:
public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
В своем тесте вы создаете новый Reader с имитацией ввода и заменяете System.in
им. Проблема в том, что этот статический элемент создается только один раз и не заменяется новым макетом. Это заставляет его возвращать ноль при последующих чтениях, по умолчанию равным 1, что приводит к неправильному выводу.
В качестве исправления я бы предложил удалить статические данные, ввести читатель, чтобы его можно было заменить, и использовать main только как корень композиции.
public class TriTyp {
private static String[] triTypes = {"", // Ignore 0.
"scalene", "isosceles", "equilateral", "not a valid triangle"};
private static String instructions = "This is the ancient TriTyp program.\nEnter three integers that represent the lengths of the sides of a triangle.\nThe triangle will be categorized as either scalene, isosceles, equilateral\nor invalid.\n";
private final BufferedReader in;
public TriTyp(BufferedReader in) {
this.in = in;
}
public static void main(String[] argv) {
new TriTyp(new BufferedReader(new InputStreamReader(System.in))).run();
}
public void run() {
int A, B, C;
int T;
System.out.println(instructions);
System.out.println("Enter side 1: ");
A = getN();
System.out.println("Enter side 2: ");
B = getN();
System.out.println("Enter side 3: ");
C = getN();
T = triang(A, B, C);
System.out.println("Result is: " + triTypes[T]);
}
// Triang and getN methods. Just drop their static keyword.
}
И в тестах вы бы заменили это:
String mockinput = "3\r\n3\r\n3";
ByteArrayInputStream in = new ByteArrayInputStream(mockinput.getBytes());
// Set up mock user input stream
System.setIn(in);
// Initialize the TriTyp.main(input) method
String[] mainin = {};
TriTyp.main(mainin);
С этим:
String mockinput = "3\r\n3\r\n3";
ByteArrayInputStream in = new ByteArrayInputStream(mockinput.getBytes());
new TriTyp(new BufferedReader(new InputStreamReader(in))).run();