David Kubecka
11/20/2024, 9:35 AMcontext(Account)
@ExtendWith(TestAccountParameterResolver::class) // injects Account value
fun myTest() {
// call a function with Account context
}
This relies on the fact that contexts are compiled down as first parameters of the "annotated" method. The reason I'm using this trick instead of simply doing with(TEST_ACCOUNT)
inside the body is that with multiple contexts you get multiple levels of indentation which doesn't look very nice. Unfortunately, this doesn't play well with parametrized tests
context(Account)
@ExtendWith(TestAccountParameterResolver::class) // injects Account value
@ParameterizedTest(name = "{0}")
@MethodSource("intTestProvider")
fun myTest(input: Int) {
// call a function with Account context
}
because junit ParameterizedTestParameterResolver
injects the parameters simply according to index. Therefore the values from the intTestProvider
are tried to be injected into the actual first Account parameter, leading to multiple-matching-resolvers junit error.
This seems pretty hopeless but I would still like to ask if anyone can come up with a way how to use context receivers along parametrized tests, even a hacky one. Or more generally, how to prevent that ugly nested with
chain. Perhaps there's other way than using parameter resolvers...