Question about context receivers and their usage t...
# getting-started
d
Question about context receivers and their usage together with junit parametrized tests. For normal (non-parametrized) tests I'm currently using junit ParameterResolver to inject a context value into a test method
Copy code
context(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
Copy code
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...