Justin
09/17/2020, 4:15 AMkotlinx.cli
)?
The app itself looks pretty typical, e.g.:
fun main(args: Array<String>) {
// Do stuff that might exit with status code 0 or 1
}
My (apparently wrong) instinct was to write tests like:
@Test fun testExitsWithoutError() {
val result = main(args)
assertEquals(0, result)
}
But main
can only return void
. How do I write tests that can pass args into main and expect a certain result?nanodeath
09/17/2020, 4:23 AMnanodeath
09/17/2020, 4:23 AMJustin
09/17/2020, 4:26 AMnanodeath
09/17/2020, 4:29 AMSystem.exitProcess
or something, but I think it's better design to just create modular stateless classes (when possible) and test those individually, with maybe a couple "integration tests" that do actually invoke mainJames Richardson
09/17/2020, 8:00 AMJustin
09/17/2020, 3:46 PMJames Richardson
09/17/2020, 3:48 PMJames Richardson
09/17/2020, 3:51 PMJustin
09/17/2020, 5:15 PMIf it exits with a status, check the status.This is exactly what I am trying to do. I guess my question is, how do I capture the exit status code from a kotlin @Test ?
nanodeath
09/17/2020, 5:17 PMJustin
09/17/2020, 5:18 PMJustin
09/17/2020, 5:18 PMJames Richardson
09/17/2020, 5:19 PMnanodeath
09/17/2020, 5:19 PMJames Richardson
09/17/2020, 5:30 PMJames Richardson
09/17/2020, 5:31 PMimport com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
class MainclassTest {
@org.junit.Test
fun `process exits with code 0`() {
val status = ProcessBuilder(
System.getProperty("java.home") + "/bin/java",
"-classpath",
System.getProperty("java.class.path"),
MainclassTest::class.qualifiedName + "Kt"
).start().waitFor()
assertThat(status, equalTo(0))
}
}
fun main() {
println("Hello")
}
James Richardson
09/17/2020, 5:31 PMJames Richardson
09/17/2020, 5:31 PMJames Richardson
09/17/2020, 5:33 PMJustin
09/17/2020, 5:53 PMJames Richardson
09/17/2020, 5:56 PM