This is the problem:
Program A is a Reader, its waiting using a reader.read() around System.in. We need to write autotest for it. Another program, a Writer, should be able to write to this Reader, that is, write to it as if the user is punching text.
We want to "pump" text into the input stream via another program, not via the keyboard.
No. It's a shell thing. For a demo, here are two simple programs:
import java.io.*;
public class Reading {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null)
System.out.format("Reading sees [%s]%n", line);
}
}
and
public class Writing {
public static void main(String[] args) {
for (int i = 0; i < 10; ++i)
System.out.format("This is line %d%n", i);
}
}
Once compiled, you run them from the command line by:
% java Writing | java Reading
... or however you shell expressed piping the output from one process to be the input of another.
Try that!
Hunter is right, too. You could first redirect the writer's output to a file:
$java Writing > w.txt
Then run the reader using that input:
$java Reading < w.txt
Sometimes this doesn't work -- say the programs have to run for a long time, and
then you'll have to run them concurrently, with the pipe.
UNIX guys do this all the time.
I would use a text file as the input source, but that may not be the best solution depending on how you read the data in your code. Add a line to the file that is the user's first line of input:
Hi, I'm a user.
This is my second input line
etc...
Message was edited by:
hunter9000
Try something like this (Running on WinXP w/ Cygwin, via bash):
Test.java:
import java.io.*;
import java.util.*;
public class Test{
public static void main(String[] args) {
System.out.print("Type 'ok' to continue: ");
Scanner scanner = new Scanner(System.in);
String next = scanner.next();
if (next.equals("ok")) {
System.out.println("\n Yay...");
} else {
System.out.println("\n Oh poo...");
}
}
}
test.txt (Just the text "ok" with a newline following):
ok
test2.txt (Just the text "boo" with a newline following):
boo
kevin.wallace@MOKANSASC2DA77 ~/java$ java Test < test.txt
Type 'ok' to continue:
Yay...
kevin.wallace@MOKANSASC2DA77 ~/java$ java Test < test2.txt
Type 'ok' to continue:
Oh poo...
I don't know what the equivalent without Cygwin's bash shell would look like.
> You guys ever heard of JUnit testing?
>
> Guys, I am trying to Drive my console application
> using JUnit. It should be able to punch in text
> dynamically.
To automate the tests in JUnit, you could point your Reader to a FileInputStream with your test file, but then you wouldn't be testing the console input part, just what you do once you have the input. <shrug>
> Guys, I am trying to Drive my console application
> using JUnit. It should be able to punch in text
> dynamically.
Maybe if you passed in your InputStream object to the console app part, and used a driver to instantiate it (I haven't tested this)...
public class Myapp {
InputStream in = null;
public Myapp() {
this.in = System.in;
doConsoleThing();
}
public Myapp(InputStream in) {
this.in = in;
doConsoleThing();
}
public static void main(String[] args) {
Myapp app = new Myapp();
}
public void doConsoleThing() {
// Do your nifty console thing...
}
Your JUnit test would do something like:
public void testMyapp() {
FileInputStream fis = new FileInputStream( new File("test.txt") );
Myapp app = new Myapp(fis);
}