https://kotlinlang.org logo
#minutest
Title
# minutest
c

christophsturm

12/01/2023, 6:51 PM
hey I am thinking about minutest style fixtures (or givens) and I have one problem that puzzles me: what do I do when one field of my fixture depends on a different field of my fixture and I don’t want to make my fixture nullable. heres a failgood test:
Copy code
val context = describe {
        val assertionError: AssertionError = AssertionError("failed")
        describe("with a typical valid root context") {
            val ctx =
                RootContext("root context") {
                    test("failed test") { throw assertionError }
                }
            describe("executing all the tests") {
                val contextInfo = assertNotNull(execute(ctx) as? ContextInfo)
                describe("reports failed tests") {
                    val failure =
                        contextInfo.tests.values
                            .awaitAll()
                            .map { it.result }
                            .filterIsInstance<Failure>()
                            .single()
                    it("reports exception for failed tests") {
                        expectThat(failure.failure) {
                            get { stackTraceToString() }
                                .isEqualTo(assertionError.stackTraceToString())
                        }
                    }
                }
so the fixture is basically the rootcontext (ctx), and the assertionError (because in the test I need to have the assertion error to check that exactly that error is reported as failure) when I try to convert this to a given style (with my own made up test dsl) I have to make the context field nullable and mutable, because I need the given class constructed to reference the exception in my context.
Copy code
data class Given(
        val assertionError: java.lang.AssertionError,
        var context: RootContext? = null
    )

    val test =
        context(
            "contextExecutorTest port",
            given = {
                val given = Given(AssertionError("failed"))
                given.context =
                    RootContext("root context") {
                        test("failed test") {
                            throw given.assertionError
                        }
                    }
                given
            }
        )
is there a better way to do this and avoid making the context nullable?
ok I found the solution, just use a normal class
Copy code
class Given {
        val assertionError: java.lang.AssertionError = AssertionError("failed")
        val context: RootContext =                    RootContext("root context") {
            test("failed test") {
                throw assertionError
            }
        }

    }
    and then just given={Given()}