Martin Thoma
Martin Thoma

Reputation: 136735

How can I test input and output of a Java Application?

I have a simple Java console application and would like to test its input / output automatically. The input is always only one line, but the output is sometimes more than one line.

How can I do this? (with a Linux shell / Python / Eclipse / Java)

Upvotes: 0

Views: 220

Answers (3)

John Eipe
John Eipe

Reputation: 11236

You can execute any Unix command using watch command. Watch command will be executed until you terminate it either by CTRL+C or kill the process.

$ watch -n 5 ls

By default watch command uses 2 second interval, you can change it using -n option.

Or you could write a function like this in your .bashrc (from here)

function run() {
    number=$1
    shift
    for i in {1..$number}; do
      $@
    done
}

And use it like this

run 10 command

Upvotes: 0

JProgrammer
JProgrammer

Reputation: 1135

In eclipse you can log your console output to a physical file using the Run configuration settings. Run-> Run Configuration-> Select your application->go to common tab-> in 'Standard input and output' section specify physical file path.

Upvotes: 1

Oleksi
Oleksi

Reputation: 13097

You could use pipes in Linux. For example, run your problem like this:

java myProgram < input_file > output_file

This will run myProgram and feed input from input_file. All output will be written to a file called output_file.

Now create another file called expected_file which you should handcreate to specify the exact output you expect on some input (specifically, the input you have in input_file).

Then you can use diff to compare the output_file and the expected_file:

diff output_file expected_file

This will output any differences between the two files. If there are no differences, nothing will be returned. Specifically, if something gets returned, your program does not work correctly (or your test is wrong).

The final step is to link all these commands in some scripting language like Ruby (:)) or Bash (:().

This is the most straight-forward way to do this sort of testing. If you need to write more tests, consider using some test frameworks like junit.

Upvotes: 7

Related Questions