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.

7
  • MVC, MVP ? Any design patterns? Commented Apr 17, 2013 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. Commented Apr 17, 2013 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. Commented Apr 17, 2013 at 18:02
  • Yes but how do I test it? Commented Apr 17, 2013 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. Commented Apr 17, 2013 at 18:13

3 Answers 3

8

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.
Sign up to request clarification or add additional context in comments.

3 Comments

Could you please share an example using System.setIn? Thanks
This only works for one input. If you want multiple inputs (i.e. the program prompts for input a few times, it doesn't work)
@Imray This does work, see updated examples. You just need to put all of your inputs in the input data.
1

If on unix

java MyProgram < sampleinput.txt

4 Comments

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 this redirects stdin to read from the sample text file instead of from console.
Yes I understand that. And that is why I said I am not reading from file but from console
@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.
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())));

}

6 Comments

I know about scanner class but how do I test it. In my test class I create class instance: MyClass1 numbers = new numbers(1,2,3) //which is the default constructor. I cannot do something like numbers(System.nextInt(), ...).
edited the answer. get the user input and hold them in variable then pass it to the constructor
use scan.nextInt(); if you want int input from user
Using System.in in a unit test does not make much sense from my point of view.
But he has that requirement to take the input from user. Thats how you take the input from user in Java. Ideally either hard code the data or use some kind of mock framework.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.