Ravin Jain
10/14/2021, 7:00 AMany<File>().outputStream()
I am trying this
val mockOs = mockk<FileOutputStream>()
mockkStatic("kotlin.io.FilesKt__FileReadWriteKt")
mockkStatic(Files::class)
val resultFile = mockk<File>()
every { resultFile.path } answers {"somepath"}
every { any<File>().outputStream() } returns mockOs
But always gets this error
(No such file or directory)
java.io.FileNotFoundException: (No such file or directory)
at java.base/java.io.FileOutputStream.open0(Native Method)
at java.base/java.io.FileOutputStream.open(FileOutputStream.java:298)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:237)
at java.base/java.io.FileOutputStream.<init>(FileOutputStream.java:187)
Any idea ?Mattia Tommasone
10/14/2021, 7:56 AMevery { resultFile.outputStream() } returns mockOs
You cannot use any<File>()
that way: any() is to match arguments, not to describe mocksRavin Jain
10/14/2021, 4:07 PMephemient
10/14/2021, 7:32 PMmockkStatic(File::outputStream)
2. in this case, any<File>().outputStream()
is correct because the File
is the receiver of an extension function, which makes it just like a normal argument in every way except syntax
3. I'm pretty sure the problem here is that FileOutputStream
is from the system classloader and cannot be mockedephemient
10/14/2021, 8:49 PMMatteo Mirk
10/15/2021, 10:27 AMephemient
10/15/2021, 11:32 AMephemient
10/15/2021, 11:34 AMMatteo Mirk
10/15/2021, 11:34 AMRavin Jain
10/26/2021, 9:30 PMMatteo Mirk
10/27/2021, 8:56 AMephemient
10/27/2021, 8:59 AM