junit--輸入相關的測試

一般使用輸入時都使用Scanner

Scanner scanner=new Scanner(System.in);
while(scanner.hasNext()){
    String next=scanner.next();
}

爲了在單元測試中無需人的參與,可以使用如下的方式

System.setIn(new ByteArrayInputStream("what".getBytes()));

配合groovy食用更佳

@Test
void readNormal() {
    String inputMessage = "hello"
    input.scanner = new Scanner(new ByteArrayInputStream((inputMessage + input.delimiter).getBytes()))
    assertTrue(input.ready())
    assertEquals(input.read().get(Event.MESSAGE), inputMessage)
}

一個基本的單元測試就完成了
完整的例子歡迎來github
https://github.com/lT1161558158/shipper-core

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章