mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-03-14 11:50:28 +08:00
add case to test kotlin invoke java method
This commit is contained in:
parent
2ad671e1f9
commit
419a10669d
@ -0,0 +1,27 @@
|
||||
package com.alibaba.testable.demo
|
||||
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
|
||||
object PathUtil {
|
||||
|
||||
fun deleteRecursively(file: File) {
|
||||
if (!file.exists()) {
|
||||
return
|
||||
}
|
||||
val fileList = file.listFiles()
|
||||
if (fileList != null) {
|
||||
for (childFile in fileList) {
|
||||
if (childFile.isDirectory) {
|
||||
deleteRecursively(childFile)
|
||||
} else if (!childFile.delete()) {
|
||||
throw IOException()
|
||||
}
|
||||
}
|
||||
}
|
||||
if (file.exists() && !file.delete()) {
|
||||
throw IOException("Unable to delete file " + file.absolutePath)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.alibaba.testable.demo
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import com.alibaba.testable.core.annotation.TestableMock
|
||||
import com.alibaba.testable.core.tool.TestableTool.verify
|
||||
import java.io.File
|
||||
|
||||
class PathUtilTest {
|
||||
|
||||
@TestableMock
|
||||
fun exists(f: File): Boolean {
|
||||
return when (f.absolutePath) {
|
||||
"/a/b" -> true
|
||||
"/a/b/c" -> true
|
||||
else -> f.exists()
|
||||
}
|
||||
}
|
||||
|
||||
@TestableMock
|
||||
fun isDirectory(f: File): Boolean {
|
||||
return when (f.absolutePath) {
|
||||
"/a/b/c" -> true
|
||||
else -> f.isDirectory
|
||||
}
|
||||
}
|
||||
|
||||
@TestableMock
|
||||
fun delete(f: File): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
@TestableMock
|
||||
fun listFiles(f: File): Array<File>? {
|
||||
return when (f.absolutePath) {
|
||||
"/a/b" -> arrayOf(File("/a/b/c"), File("/a/b/d"))
|
||||
"/a/b/c" -> arrayOf(File("/a/b/c/e"))
|
||||
else -> f.listFiles()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun should_able_to_mock_java_method_invoke_in_kotlin() {
|
||||
PathUtil.deleteRecursively(File("/a/b/"))
|
||||
verify("listFiles").times(2)
|
||||
verify("delete").times(4)
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user