Testing console based applications/programs - Java

前端 未结 5 1461
故里飘歌
故里飘歌 2020-12-03 05:47

All,

I have written a PhoneBook application in Java that is command line based. The application basically asks for some details of user like Name, Age, Address and p

5条回答
  •  星月不相逢
    2020-12-03 06:24

    The library System Rules provides the rule TextFromStandardInputStream for simulating input in JUnit tests.

    public class YourAppTest {
      @Rule
      public TextFromStandardInputStream systemInMock = emptyStandardInputStream();
    
      @Test
      public void test() {
        systemInMock.provideText("name\nsomething else\n");
        YourApp.main();
        //assertSomething
      }
    }
    

    For details have a look at the System Rules documentation.

提交回复
热议问题