0

How to test an interactive java application using cucumber and aruba

I have this Cucumber/Aruba scenario:

Scenario: Simple echo
  Given I run `java -cp /bin Echo` interactively
  When I type "something\n"
  Then the output should contain "something"

To test this simple java program:

import java.util.*;
class Echo {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        System.out.println(stdin.next());
    }
}

When I run the test it fails and I get the following error:

Then the output should contain "something"
    expected "" to include "something" (RSpec::Expectations::ExpectationNotMetError)
    features/cli.feature:15:in `Then the output should contain "something"'

When I try the same test with this ruby program:

puts gets

is all green...

Am I doing something wrong here?

Do have to use another method to read from standard input? is System.in used for the keyboard only?

victrnava
  • 1,134
  • 1
  • 9
  • 13

1 Answers1

0

A couple of things you could try:

First test that Cucumber/Aruba is picking up your System.out.println call by forcing the output to be what you expect in your test: i.e.

System.out.println("something");

If that works, then you hunch on reading standard input is correct (i have never seen someone read from standard input the way you are doing it). The answer below is how I typically see people read from standard input in Java:

How to read input with multiple lines in Java

Community
  • 1
  • 1
Clinton
  • 3,638
  • 3
  • 26
  • 33
  • So System.out.println("Something") is still not getting picked up and making your test pass? Sounds like aruba isn't picking up the Java output. Does it pick up content from System.err.println("Something")? – Clinton Mar 30 '11 at 00:27