多次测试主方法 - Java JUnit

Test Main Method Multiple Times- Java JUnit

我需要通过不同的测试多次使用用户输入模拟测试我的主要方法。我能够测试该方法一次,但是当我第二次测试它时,我无法这样做。如果我不调用主要方法,则什么也不会发生,但如果我进行测试,则基本上不会 运行 并且会错过我的断点。

我的密码是

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);
    }

}

主要方法的代码是

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();

    }

问题出在此处的 test2 方法中:

    @Test
    public void test2() //Add Duplicate Paper
    {
        WorkshopReviewSystem.main(null);

main 方法在未设置 InputStream 的情况下被调用(与 test1() 不同),因此,当控件到达 [=12= 中的 Scanner in = new Scanner(System.in); 行时,系统会一直等待用户输入].

我们需要在调用 main 之前将 InputStream 设置为 test1()