6

I am trying to understand how can I test a user's input (please note I am not trying to do a mock test but a test of actual user's input)

Currently as you may see in my program I have hard coded the values for my test case and it is passing all tests but how can I get a user's input and test it .

Is there a way where I can call System.in my constructor and pass it when I create an instance of MyClass1 in the test class?

Please, if possible give me some example code so I can better understand.

If I have a interface as such

public interface IMyClass{
   public int getvalue1();
   public int getvalue2();
   public int getvalue3();
}

and then interface implementation as such

public class MyClass1 implements MyClass{

private int _value1 = 0;
private int _value2 = 0;
private int _value3 = 0;



public MyClass1(int number1, int number2, int number3)
{

   _value1 = number1;
    _value2 = number2;
    _value3 = number3;
}

public void setLength1(int value1)
{
    _value1 = value1;
}

public void setLength2(int length2)
{
    _value2 = value2;
}

public void setLength3(int length3)
{
    _value3 = value3;
}

public int getValue1()
{
    return _value1;
}

public int getValue2()
{
    return _value2;
}

public int getValue3()
{
    return _value3;
}
}

and finally a test class as such:

public class ClasTest extends TestCase {

public void testNumbers()
{
   MyClass1 numbers= new MyClass1(1,2,3);
   assertThat(numbers.getValue1(),is(not(numbers.getValue2())));

}
}

Thankyou, I appreciate any help.

talonmies
  • 67,081
  • 33
  • 170
  • 244
user1462617
  • 345
  • 1
  • 7
  • 15
  • MVC, MVP ? Any design patterns? –  Apr 17 '13 at 17:47
  • What you do in your test is sufficient from a testing point of view. The only thing possible would be to randomly generate input and test based on that random input. – Markus Apr 17 '13 at 17:56
  • 2
    Why not use hardcoded values for your tests? You just need to think about the special cases (boundaries like Integer.MAX_VALUE, 0 etc) also mix ins some positive and negative numbers and maybe this will be enough. This basic cases should cover all other cases. – hovanessyan Apr 17 '13 at 18:02
  • Yes but how do I test it? – user1462617 Apr 17 '13 at 18:05
  • 2
    Test what exactly? You've not shown any code under test that involves user input. Having a unit test itself reading from the console in any way (System.in, Scanner, ...) is not how unit testing works -- unit tests are supposed to be automated and not rely on human interaction. – Philipp Reichart Apr 17 '13 at 18:13
  • Voting to close as not a real question. Between the comments on this and all the answers it's clear no one has any idea what you are talking about. – djechlin Apr 17 '13 at 18:38
  • Unit tests for setters and getters that operate on a single value without any business rules are hardly useful – Sami Korhonen Apr 17 '13 at 19:52

3 Answers3

7

Use System.setIn(new InputSteam()); and then write to the input stream passed in to System.in

see: JUnit: How to simulate System.in testing?

Single Input

String data = "Users Input";
System.setIn(new ByteArrayInputStream(data.getBytes()));

Scanner scanner = new Scanner(System.in);
System.out.println(scanner.nextLine());

Result

Users Input

Multiple Inputs

String data = "Users Input" +
        "\nA second line of user input.";
System.setIn(new ByteArrayInputStream(data.getBytes()));

Scanner scanner = new Scanner(System.in);
System.out.println("Line 1: " + scanner.nextLine());
System.out.println("Line 2: " + scanner.nextLine());

Result

Line 1: Users Input
Line 2: A second line of user input.
Community
  • 1
  • 1
Mike Rylander
  • 16,938
  • 22
  • 81
  • 134
1

If on unix

java MyProgram < sampleinput.txt

djechlin
  • 54,898
  • 29
  • 144
  • 264
  • I am not on unix and dont know much unix. And I am not reading from .txt file. I am reading from console. Thanks though – user1462617 Apr 17 '13 at 18:07
  • @user1462617 this redirects stdin to read from the sample text file instead of from console. – djechlin Apr 17 '13 at 18:26
  • Yes I understand that. And that is why I said I am not reading from file but from console – user1462617 Apr 17 '13 at 18:34
  • @user1462617 then every time you run a test you'll need to hire someone to type the input into the console. I don't know how you expect to test without using a file with test input. – djechlin Apr 17 '13 at 18:37
0

user Scanner class

public void testNumbers()
{

    Scanner scan = new Scanner(System.in);
    System.out.println("value1");
    int value1 = scan.nextInt();

    System.out.println("value2");
    int  value2 = scan.nextInt();

    System.out.println("value3");
    int value3 = scan.nextInt();

   MyClass1 numbers= new MyClass1(value1, value2, value3);
   assertThat(numbers.getValue1(),is(not(numbers.getValue2())));

}
NullPointerException
  • 3,419
  • 5
  • 21
  • 53