Functional Testing


fun sayHello() { 
  System.out.println(readLine()?.let { 
    "Hello, " + it
  }.orElseEmpty())
}


fun sayHello(
    printFn: (String) -> Unit = System.out::println, 
    readFn: () -> String? = ::readLine) { 
  printFn(readLine()?.let { 
    "Hello, " + it
  }.orElseEmpty())
}

@Test 
fun `test sayHello`() { 
  val sb = StringBuilder()

  sayHello(
    printFn = { sb.append(it) },
    read = {  "input" })
  assertEquals("Hello, input", sb.toString())
}

Back Home Next