多次测试主要方法 - Java JUnit

时间:2016-03-15 23:07:50

标签: java testing junit automated-tests integration-testing

我需要使用不同的测试多次多次使用用户输入仿真来测试我的main方法。我能够测试一次该方法,但是当我第二次测试它时,我无法这样做。如果我不打电话给主方法没有任何反应,但是如果我做的话测试基本上没有运行并且错过了我的断点。

我的代码是

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

    }

This is the image of the debug window of when the program is at the brakepoint for assertequals on test 1

This is the image of the debug window of when the program is at the brakepoint for assertequals on test 2

1 个答案:

答案 0 :(得分:1)

问题在于test2方法:

    @Test
    public void test2() //Add Duplicate Paper
    {
        WorkshopReviewSystem.main(null);
在没有设置InputStream的情况下调用

main方法(与test1()不同)因此,当控件到达Scanner in = new Scanner(System.in);中的main行时,系统会一直等待用户的输入。

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