JUnit test for System.out.println()

前端 未结 13 1724
广开言路
广开言路 2020-11-22 03:18

I need to write JUnit tests for an old application that\'s poorly designed and is writing a lot of error messages to standard output. When the getResponse(String reque

13条回答
  •  故里飘歌
    2020-11-22 04:12

    I know this is an old thread, but there is a nice library to do this:

    System Rules

    Example from the docs:

    public void MyTest {
        @Rule
        public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();
    
        @Test
        public void overrideProperty() {
            System.out.print("hello world");
            assertEquals("hello world", systemOutRule.getLog());
        }
    }
    

    It will also allow you to trap System.exit(-1) and other things that a command line tool would need to be tested for.

提交回复
热议问题