Мое применение FastScanner
(на основе BufferedReader
) для чтения ввода (Посмотрите на раздел кода моего вопроса).JMockit: howto invoke custom method of MockUp
Если я хочу заменить ввод в своих тестах, я должен сделать макет FastScanner
(Посмотрите на раздел кода моего вопроса).
Задача:For each input I should make separate mockup.
Это будет greate, если я могу переключать входной сигнал внутри одного Mock Up.
Вопрос:How to add custom methods to JMockit MockUps and than call them?
(Посмотрите switchInput
метод FastScanner
макете)
Код: [Этот раздел является обязательным, только для лучшего понимания]
FastScanner
protected static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
FastScanner макете:
new MockUp<FastScanner>() {
private int[] input1 = new int[] {17, 2, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1};
private int[] input2 = new int[] {5, 2, 2, 3, 3, 3, 3, 3};
private int[] input3 = new int[] {8, 2, 5, 1, 3, 1, 1, 1, 1, 3, 1};
private byte toggler = 1;
private byte pointer = 0;
//HERE THE QUESTION: HOW CAN I CALL THIS METHOD
public void switchInput() {
toggler++;
pointer = 0;
}
@SuppressWarnings("unused")
int nextInt() throws IOException {
int[] input = null;
switch (toggler) {
case 1: input = input1; break;
case 2: input = input2; break;
case 3: input = input3; break;
}
return input[pointer++];
}
};
Вы попробовали? Каков был результат? – Andy
@ Энди, как я могу попробовать? Я не могу ставить статичность 'switchInput' и не входит в API FastScanner. Так что Java просто не видит этот метод во время компиляции –
О, вы хотите вызвать '' switchInput() '' извне макета. Хм, зачем вообще это делать? почему бы не создать 3 разных макета? – Andy