https://kotlinlang.org logo
Title
d

Davide Giuseppe Farella

09/24/2020, 11:01 AM
Hello, I have a class like
class MyClass(val dir: File) {

  fun stuff(filesNames: List<String>) {
    ...
    val file = File(dir, fileName)
    if (file.exists())
    ...
  }
}
What would be the right way in order to pass the check
if (file.exists())
?
v

vitaliy

09/24/2020, 12:18 PM
in isolation it looks like a use case for
mockkClass
. Example from https://mockk.io/ :
val car = mockkClass(Car::class)

every { car.drive(Direction.NORTH) } returns Outcome.OK

car.drive(Direction.NORTH) // returns OK

verify { car.drive(Direction.NORTH) }
I’m not sure though if this would work when File is instantiated inside of the method under test
d

Davide Giuseppe Farella

09/24/2020, 12:19 PM
But child File is created internally from the class, anyway I solved with
@get:Rule
val folder: TemporaryFolder = TemporaryFolder()
🚀 1