Ideally, extract the awkward dependencies so that you can test without them. Change main
to simply:
public static void main(String[] args) {
doWork(new Scanner(System.in), System.out);
}
// TODO: Rename to something meaningful
public static void doWork(Scanner input, PrintStream output) {
// Remainder of code
}
(Consider using a Writer
instead of a PrintStream
for output
.)
Then you don't really need to unit test main
- but you can test doWork
using a Scanner
based on a StringReader
, and output based on a StringWriter
, providing whatever input you want and checking the output.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…