I am trying to test one method from a program.
Method:
private static int dateValidation(Scanner sc)
{
int date;
do
{
System.out.println("Please enter date of birth in numbers:");
date = sc.nextInt();
sc.nextLine();
if (date < 1 || date > 31)
{
System.out.println("Error. Please enter an appropriate date between 1-31 inclusive.");
}
} while (date < 1 || date > 31);
return date;
}
And I am testing stimulated user input by doing this:
import java.io.*;
public class programTest
{
public static void dateValidationTest()
{
String inputTest = "10";
System.setIn(new ByteArrayInputStream(inputTest.getBytes()));
assertEquals(10, program.dateValidation());
}
}
Which gives me this error as dateValidation requires Scanner as input
programTest.java:11: error: method dateValidation in class program cannot be applied to given types;
assertEquals(10, program.dateValidation());
^
required: Scanner
found: no arguments
reason: actual and formal argument lists differ in length
1 error
However when I add inputTest into it like assertEquals(10, program.dateValidation(inputTest)), another error occurs:
programTest.java:11: error: incompatible types: String cannot be converted to Scanner
assertEquals(10, program.dateValidation(simInput));
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error
I am not sure if I am even doing the testing correctly, so I was wondering what is the correct way to do this?