繁体   English   中英

多次测试主要方法-Java JUnit

[英]Test Main Method Multiple Times- Java JUnit

我需要使用不同的测试多次使用用户输入仿真来测试我的主要方法。 我能够测试一次该方法,但是当我第二次测试时,我无法进行测试。 如果我不调用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();

    }

这是调试窗口的图像,当程序在测试1上达到断言等于制动点时

这是调试窗口的图像,当程序在测试2上达到断言等于制动点时

问题在这里的test2方法中:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM