【发布时间】:2020-05-03 16:55:48
【问题描述】:
如何在 kotlin 中测试执行如下 readline 的代码:
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.*
import java.io.*
fun foo() {
val string="a b\nc d"
var bais: ByteArrayInputStream=string.byteInputStream()
val bis=BufferedInputStream(bais)
//var l=bis.readLine() // no readline!
// program uses just readline()
var br: BufferedReader =BufferedReader(InputStreamReader(bais))
var l=br.readLine()
println(l) // works file
val si=System.`in`
println("System.`in` is ${si::class.simpleName}") // says it's a BufferedInputStream
}
fun main() {
foo()
}
@TestInstance(TestInstance.Lifecycle.PER_CLASS) internal class T1KtTestCase {
@Test fun testFoo() {
// ?
}
}
【问题讨论】:
-
您正在使用 java 框架,在
System方法的 kotlin 测试中与 java 没有区别。检查这个stackoverflow.com/questions/1119385/… -
是的。看起来我很兴奋,因为我需要模拟 readLine()
标签: kotlin intellij-idea junit5 system.in junit-jupiter