I may use case the class under test has many cases so it is divided into a structure of inner classes. I want to write parameterized test cases to reduce boiler plate and code duplication. For this I wanted to go with the approach of method source. Class under test
class UnderTest
{
testThisMethod(a:String,b:String?){
// Do something
externalInterface.call(a?:b)
}
}
Test Case structure
internal class A() {
private val externalService = mockk<ExternalService>
private val test: UnderTest(externalService)
// Some general tests
//---Position Outter
inner class A {
//--- Position A
inner class B {
//--- Position C
@ParameterizedTest
@MethodSource("provideArguments")
fun `with arguments external service create object`(
argument1: String,
argument2: String,
expected: String
) {
// Some code
verify {
externalService.call(expected)
//some more verification
}
}
}
}
}
To provide the argument provider method I tried placing it at positions and got following errors
- Position outer: initialization error :Could not find factory method in class
- Position A,B: compilation error: companion not allowed here
How can this be achieved?
Try using
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class A() { private val externalService = mockk private val test: UnderTest(externalService)
}
Check here for more details