add case to test kotlin invoke java method

This commit is contained in:
金戟 2020-10-26 20:39:12 +08:00
parent 2ad671e1f9
commit 419a10669d
2 changed files with 75 additions and 0 deletions

View File

@ -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)
}
}
}

View File

@ -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)
}
}