Reputation: 490
I need to test my main method with user input emulation multiple times multiple times with different tests. I am able to test the method once but when I test it a second time I am unable to do so. If i don't call the main method nothing happens but if i do the test essentially does not run and misses my breakpoints.
My code is
public class TestC
{
WorkshopReviewSystem workshop = new WorkshopReviewSystem();
WorkshopReviewSystem workshop2 = new WorkshopReviewSystem();
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private void write(String input)
{
InputStream stdin = System.in;
System.setIn(new ByteArrayInputStream(input.getBytes()));
System.setIn(stdin);
}
@Test
public void test()
{
InputStream stdin = System.in;
System.setIn(new ByteArrayInputStream("J".getBytes()));
workshop.main(null);
//WorkshopReviewSystem.main(null);
System.setIn(stdin);
System.setOut(new PrintStream(outContent));
String testS = outContent.toString();
assertEquals(outContent.toString().contains("Something went wrong:"), true);
}
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
}
@After
public void cleanUpStreams() {
System.setOut(null);
}
@Test
public void test2() //Add Duplicate Paper
{
WorkshopReviewSystem.main(null);
InputStream stdin = System.in;
System.setIn(new ByteArrayInputStream("P".getBytes()));
System.setIn(stdin);
System.setIn(new ByteArrayInputStream("Test Paper".getBytes()));
System.setIn(stdin);
System.setIn(new ByteArrayInputStream("P".getBytes()));
System.setIn(stdin);
System.setIn(new ByteArrayInputStream("Test Paper".getBytes()));
System.setOut(new PrintStream(outContent));
System.setIn(stdin);
/*write("P");
write("Test Paper");
write("P");
write("Test Paper");*/
String testS = outContent.toString();
assertEquals(outContent.toString().contains("Paper Already Added"),true);
}
}
The code for the main method is
public static void main(String[] args) {
// TODO Auto-generated method stub
//////////////
//example test data
//////////////
AllPapers = new ArrayList<WorkshopPaper>();
WorkshopPaper p1 = new WorkshopPaper("Paper 1 is great");
p1.addReview(new WorkshopReview(4,"This paper is pretty good."));
p1.addReview(new WorkshopReview(3,"This paper is good for the workshop."));
p1.addReview(new WorkshopReview(2, "This paper is pretty mediocre."));
AllPapers.add(p1);
WorkshopPaper p2 = new WorkshopPaper("Paper 2 is my best work");
p2.addReview(new WorkshopReview(2,"This can hardly be his best work"));
p2.addReview(new WorkshopReview(1,"Ive read better articles in Hello Magazine"));
p2.addReview(new WorkshopReview(1,"So painful to read."));
AllPapers.add(p2);
WorkshopPaper p3 = new WorkshopPaper("Paper 2 is my best work");
p3.addReview(new WorkshopReview(2,"This can hardly be his best work"));
p3.addReview(new WorkshopReview(1,"Ive read better articles in Hello Magazine"));
p3.addReview(new WorkshopReview(1,"So painful to read."));
AllPapers.add(p3);
//PrintPaperOverview();
//PrintAPaper(0);
//PrintAPaper(1);
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()){
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) {
System.out.println("Something went wrong: " + e.toString() + "\n");
}
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");
}
in.close();
}
Upvotes: 0
Views: 1497
Reputation: 30819
The problem is in test2
method here:
@Test
public void test2() //Add Duplicate Paper
{
WorkshopReviewSystem.main(null);
main
method is being called without setting up the InputStream (unlike test1()
) and hence, system keeps waiting for user's input when control reaches Scanner in = new Scanner(System.in);
line in main
.
We need to set the InputStream
like test1()
before calling main.
Upvotes: 1