【问题标题】:Test Main Method Multiple Times- Java JUnit多次测试主要方法-Java JUnit
【发布时间】:2016-03-15 23:07:49
【问题描述】:

我需要使用不同的测试多次使用用户输入仿真多次测试我的主要方法。我可以测试一次该方法,但是当我第二次测试它时,我无法这样做。如果我不调用 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);
    }

}

main方法的代码是

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

    }

【问题讨论】:

  • 能分享一下main方法的代码吗?
  • 没错,就是这么做的。

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


【解决方案1】:

问题出在test2方法这里:

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

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

我们需要在调用 main 之前设置 InputStreamtest1() 一样。

【讨论】:

  • 但我有 InputStream stdin = System.in;在两者中
  • 需要设置之前 main被调用,在test2()中。
  • 我试过了,但是当我调试程序时,当我在 test1 上的 assertequals 处有一个刹车点时,我可以看到所有变量并且它工作正常但是当我继续在 test2 上的 assertequals 刹车点上无视它,我看不到任何变量,你知道为什么会发生吗?
  • 我添加了调试输出
  • test2() 中的断点保存在哪里?另外,您是否尝试过将主调用与输入流配置交换?
猜你喜欢
  • 2019-11-27
  • 2016-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-20
  • 1970-01-01
  • 2014-01-07
  • 2010-10-01
相关资源
最近更新 更多