1

I am writing JUnit tests and I need to emulate a User input like "P", and then I expect it to run a method. The problem is the code that I have currently does not run the method and does not show errors when I have an wrong input.

My test code is,

@Test
public void Test2(){
    String data = "J";
    boolean error2;
    System.setIn(new java.io.ByteArrayInputStream(data.getBytes()));
    error2 = WorkshopReviewSystem.GetError();
    assertEquals(true, error2);
}

The Main code is,

System.out.println("What do you want to do?\n O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit");
Scanner in = new Scanner(System.in);
while (in.hasNextLine()){
    InLoop = true;
    error = false;
    String s = in.next();
    try{
        if (s.equals("O")) {
            PrintPaperOverview();
        } else if (s.equals("P")){
            AddPaper(in);
        } else if (s.equals("R")) {
            AddReview(in);
        } else if (s.equals("X")) {
            System.out.println("Goodbye!");
            break;
        } else if (Integer.parseInt(s) != -1 ) {
            PrintAPaper(Integer.parseInt(s)-1);
        } else {
            System.out.println("Command not recognised");
        }
    } catch (Exception e) {
        error = true;
        System.out.println("Something went wrong: " + e.toString() + "\n");
}

The only console output is

0
P

How can write a test that does what I want?

Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65
Root0x
  • 472
  • 1
  • 9
  • 28

0 Answers0