這需要basic looping console application並使其可測試,使用oxbow_lakes' answer的想法。
類-正確:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class TestableLoopingConsoleExample {
public static final String INPUT_LINE_PREFIX = "> ";
public static final String EXIT_COMMAND = "exit";
public static final String RESPONSE_PLACEHOLDER = "...response goes here...";
public static final String EXIT_RESPONSE = "Exiting.";
public static void main(String[] cmdLineParams_ignored) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintStream out = new PrintStream(System.out);
PrintStream err = new PrintStream(System.err);
try {
new TestableLoopingConsoleExample().main(cmdLineParams_ignored, in, out);
} catch (Exception e) { //For real use, catch only the exactly expected types
err.println(e.toString());
}
}
...待續...
public void main(String[] cmdLineParams_ignored, BufferedReader in, PrintStream out)
throws IOException {
System.out.println("Enter some text, or '" + EXIT_COMMAND + "' to quit");
while (true) {
out.print(INPUT_LINE_PREFIX);
String input = in.readLine();
out.println(input);
if (input.length() == EXIT_COMMAND.length() &&
input.toLowerCase().equals(EXIT_COMMAND)) {
out.println(EXIT_RESPONSE);
return;
}
out.println(RESPONSE_PLACEHOLDER);
}
}
}
測試(JUnit4):
import static org.junit.Assert.assertEquals;
import static testableloopingconsoleapp.TestableLoopingConsoleExample.EXIT_COMMAND;
import static testableloopingconsoleapp.TestableLoopingConsoleExample.EXIT_RESPONSE;
import static testableloopingconsoleapp.TestableLoopingConsoleExample.INPUT_LINE_PREFIX;
import static testableloopingconsoleapp.TestableLoopingConsoleExample.RESPONSE_PLACEHOLDER;
import org.junit.Before;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.StringReader;
public class TestableLoopingConsoleExampleTest {
private final ByteArrayOutputStream out = new ByteArrayOutputStream();
private final ByteArrayOutputStream err = new ByteArrayOutputStream();
@Before
public final void resetOutputStreams() {
out.reset();
err.reset();
}
...待續...
@Test
public void testableMain_validInputFromString_outputAsExpected() throws Exception {
String line1 = "input line 1\n";
String line2 = "input line 2\n";
String line3 = "input line 3\n";
String exitLine = EXIT_COMMAND + "\n";
BufferedReader in = new BufferedReader(new StringReader(
line1 + line2 + line3 + exitLine
));
String expectedOutput =
INPUT_LINE_PREFIX + line1 +
RESPONSE_PLACEHOLDER + "\n" +
INPUT_LINE_PREFIX + line2 +
RESPONSE_PLACEHOLDER + "\n" +
INPUT_LINE_PREFIX + line3 +
RESPONSE_PLACEHOLDER + "\n" +
INPUT_LINE_PREFIX + exitLine +
EXIT_RESPONSE + "\n";
String[] ignoredCommandLineParams = null;
new TestableLoopingConsoleExample().main(ignoredCommandLineParams, in, new PrintStream(out));
assertEquals(expectedOutput, out.toString());
}
}
你可以找到一些示例代碼來做你在這裏描述的內容:http://illegalargumentexception.blogspot.com/2010/09/java-systemconsole-ides-and-testing.html但它主要是一種實現oxbow_lakes在他的回答中描述。 – McDowell 2010-11-20 10:42:27