Testing Java Command Line Program

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Testing Java Command Line Program

FitNesse mailing list
How do you test what is written to System.out and enter information into System.in using a fixture?

The first thing my program does is write to System.out using
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

writer.write(text);
writer.flush();

The program will respond in different ways, depending on what is entered in to System.in using
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

reader.readLine()

For erroneous input the program will re-prompt.
For acceptable input the program will transition to the next step and write a different prompt.

Here what I think my test should look like:

|script|choose version        |
|check |initial user prompt is|!-
Select version of game you would like to play.
Available versions: (1)USA
-!|
|when user enters text          |
|check|expected response is|!-
Select version of game you would like to play.
Available versions: (1)USA
-!|
|when user enters zero          |
|check|expected response is|!-
Select version of game you would like to play.
Available versions: (1)USA
-!|
|when user enters two           |
|check|expected response is|!-
Select version of game you would like to play.
Available versions: (1)USA
-!|
|when user enters one          |
|show|successfull transtion to next step|

and my fixture code:

package game.fitnesse;

public class ChooseVersion {

    ChooseVersion() {
    }

    public String initialUserPromptIs() {
        return getExpectedOutput();
    }

    public String expectedResponseIs() {
        return getExpectedOutput();
    }

    public String successfullTranstionToNextStep() {
        return getExpectedOutput();
    }

    public void whenUserEntersText() {
        sendInput("a");
    }

    public void whenUserEntersZero() {
        sendInput("0");
    }

    public void whenUserEntersOne() {
        sendInput("1");
    }

    public void whenUserEntersTwo() {
        sendInput("2");
    }

    private void sendInput(String input) {
        // Enter text into System.in
    }

    private String getExpectedOutput() {
        return // Read text from System.out
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: Testing Java Command Line Program

FitNesse mailing list
Why don’t you modify your production code so it takes an OutputStream to work with rather than access System.out directly?
That way you can pass in a PipedOutputStream or something which allows your fixture to read from it’s counterpart PipedInputStream.

Sebastian


> On 07 May 2016, at 11:45, Brian Gibson [hidden email] [fitnesse] <[hidden email]> wrote:
>
>
> How do you test what is written to System.out and enter information into System.in using a fixture?
>
> The first thing my program does is write to System.out using
> BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
> writer.write(text);
> writer.flush();
>
> The program will respond in different ways, depending on what is entered in to System.in using
> BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
> reader.readLine()
> For erroneous input the program will re-prompt.
> For acceptable input the program will transition to the next step and write a different prompt.
>
> Here what I think my test should look like:
> |script|choose version        |
> |check |initial user prompt is|!-
> Select version of game you would like to play.
> Available versions: (1)USA
> -!|
> |when user enters text          |
> |check|expected response is|!-
> Select version of game you would like to play.
> Available versions: (1)USA
> -!|
> |when user enters zero          |
> |check|expected response is|!-
> Select version of game you would like to play.
> Available versions: (1)USA
> -!|
> |when user enters two           |
> |check|expected response is|!-
> Select version of game you would like to play.
> Available versions: (1)USA
> -!|
> |when user enters one          |
> |show|successfull transtion to next step|
> and my fixture code:
> package game.fitnesse;
>
> public class ChooseVersion {
>
>     ChooseVersion() {
>     }
>
>     public String initialUserPromptIs() {
>         return getExpectedOutput();
>     }
>
>     public String expectedResponseIs() {
>         return getExpectedOutput();
>     }
>
>     public String successfullTranstionToNextStep() {
>         return getExpectedOutput();
>     }
>
>     public void whenUserEntersText() {
>         sendInput("a");
>     }
>
>     public void whenUserEntersZero() {
>         sendInput("0");
>     }
>
>     public void whenUserEntersOne() {
>         sendInput("1");
>     }
>
>     public void whenUserEntersTwo() {
>         sendInput("2");
>     }
>
>     private void sendInput(String input) {
>         // Enter text into System.in
>     }
>
>     private String getExpectedOutput() {
>         return // Read text from System.out
>     }
> }
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Testing Java Command Line Program

FitNesse mailing list
Additionally,

I just realised that maybe writing and reading from a stream is too much detail than is necessary for a FitNesse test.
You could move the places in your code that write to the output stream into the CLI module using some kind of abstraction.
Then your fixture can implement this abstraction as well and replace the stream based strategy entirely.

If you don’t need to write and read from streams it should be easier to implement your fixture.

- Sebastian

> On 09 May 2016, at 10:30, Sebastian Gozin <[hidden email]> wrote:
>
> Why don’t you modify your production code so it takes an OutputStream to work with rather than access System.out directly?
> That way you can pass in a PipedOutputStream or something which allows your fixture to read from it’s counterpart PipedInputStream.
>
> Sebastian
>
>
>> On 07 May 2016, at 11:45, Brian Gibson [hidden email] <mailto:[hidden email]> [fitnesse] <[hidden email] <mailto:[hidden email]>> wrote:
>>
>>
>> How do you test what is written to System.out and enter information into System.in using a fixture?
>>
>> The first thing my program does is write to System.out using
>> BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
>> writer.write(text);
>> writer.flush();
>>
>> The program will respond in different ways, depending on what is entered in to System.in using
>> BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
>> reader.readLine()
>> For erroneous input the program will re-prompt.
>> For acceptable input the program will transition to the next step and write a different prompt.
>>
>> Here what I think my test should look like:
>> |script|choose version        |
>> |check |initial user prompt is|!-
>> Select version of game you would like to play.
>> Available versions: (1)USA
>> -!|
>> |when user enters text          |
>> |check|expected response is|!-
>> Select version of game you would like to play.
>> Available versions: (1)USA
>> -!|
>> |when user enters zero          |
>> |check|expected response is|!-
>> Select version of game you would like to play.
>> Available versions: (1)USA
>> -!|
>> |when user enters two           |
>> |check|expected response is|!-
>> Select version of game you would like to play.
>> Available versions: (1)USA
>> -!|
>> |when user enters one          |
>> |show|successfull transtion to next step|
>> and my fixture code:
>> package game.fitnesse;
>>
>> public class ChooseVersion {
>>
>>     ChooseVersion() {
>>     }
>>
>>     public String initialUserPromptIs() {
>>         return getExpectedOutput();
>>     }
>>
>>     public String expectedResponseIs() {
>>         return getExpectedOutput();
>>     }
>>
>>     public String successfullTranstionToNextStep() {
>>         return getExpectedOutput();
>>     }
>>
>>     public void whenUserEntersText() {
>>         sendInput("a");
>>     }
>>
>>     public void whenUserEntersZero() {
>>         sendInput("0");
>>     }
>>
>>     public void whenUserEntersOne() {
>>         sendInput("1");
>>     }
>>
>>     public void whenUserEntersTwo() {
>>         sendInput("2");
>>     }
>>
>>     private void sendInput(String input) {
>>         // Enter text into System.in
>>     }
>>
>>     private String getExpectedOutput() {
>>         return // Read text from System.out
>>     }
>> }
>>
>>
>

Reply | Threaded
Open this post in threaded view
|

Re: Testing Java Command Line Program

FitNesse mailing list
In reply to this post by FitNesse mailing list
Hi Brian,

I think the simplest thing to do is to make the output stream configurable. That way you can write to a buffer in your tests and write to stdout by default. Dependency injection, so to speak :)

Hope this helps,

Arjan

> On 07 May 2016, at 11:45, Brian Gibson [hidden email] [fitnesse] <[hidden email]> wrote:
>
>
>
> How do you test what is written to System.out and enter information into System.in using a fixture?
>
> The first thing my program does is write to System.out using
> BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
> writer.write(text);
> writer.flush();
>
> The program will respond in different ways, depending on what is entered in to System.in using
> BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
> reader.readLine()
> For erroneous input the program will re-prompt.
> For acceptable input the program will transition to the next step and write a different prompt.
>
> Here what I think my test should look like:
> |script|choose version        |
> |check |initial user prompt is|!-
> Select version of game you would like to play.
> Available versions: (1)USA
> -!|
> |when user enters text          |
> |check|expected response is|!-
> Select version of game you would like to play.
> Available versions: (1)USA
> -!|
> |when user enters zero          |
> |check|expected response is|!-
> Select version of game you would like to play.
> Available versions: (1)USA
> -!|
> |when user enters two           |
> |check|expected response is|!-
> Select version of game you would like to play.
> Available versions: (1)USA
> -!|
> |when user enters one          |
> |show|successfull transtion to next step|
> and my fixture code:
> package game.fitnesse;
>
> public class ChooseVersion {
>
>     ChooseVersion() {
>     }
>
>     public String initialUserPromptIs() {
>         return getExpectedOutput();
>     }
>
>     public String expectedResponseIs() {
>         return getExpectedOutput();
>     }
>
>     public String successfullTranstionToNextStep() {
>         return getExpectedOutput();
>     }
>
>     public void whenUserEntersText() {
>         sendInput("a");
>     }
>
>     public void whenUserEntersZero() {
>         sendInput("0");
>     }
>
>     public void whenUserEntersOne() {
>         sendInput("1");
>     }
>
>     public void whenUserEntersTwo() {
>         sendInput("2");
>     }
>
>     private void sendInput(String input) {
>         // Enter text into System.in
>     }
>
>     private String getExpectedOutput() {
>         return // Read text from System.out
>     }
> }
>
>
>