mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-30 06:37:29 +00:00
test(config): 优化对 defaultInitializer 方法的单元测试.
将 defaultInitializer 方法的反射获取次数减少为一次, 并在测试结束后恢复访问权设置.
This commit is contained in:
parent
27f54c3c36
commit
2389d082f4
@ -64,16 +64,17 @@ internal class AppPathsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `default initializer`(@TempDir testDir: File) {
|
fun `default initializer`(@TempDir testDir: File) {
|
||||||
|
val defaultInitializerMethod = Class.forName("net.lamgc.scalabot.AppConfigsKt")
|
||||||
|
.getDeclaredMethod("defaultInitializer", AppPaths::class.java)
|
||||||
|
.apply { isAccessible = true }
|
||||||
|
|
||||||
val dirPath = "${testDir.canonicalPath}/directory/"
|
val dirPath = "${testDir.canonicalPath}/directory/"
|
||||||
val dirFile = File(dirPath)
|
val dirFile = File(dirPath)
|
||||||
mockk<AppPaths> {
|
mockk<AppPaths> {
|
||||||
every { file }.returns(File(dirPath))
|
every { file }.returns(File(dirPath))
|
||||||
every { path }.returns(dirPath)
|
every { path }.returns(dirPath)
|
||||||
every { initial() }.answers {
|
every { initial() }.answers {
|
||||||
Class.forName("net.lamgc.scalabot.AppConfigsKt")
|
defaultInitializerMethod.invoke(null, this@mockk)
|
||||||
.getDeclaredMethod("defaultInitializer", AppPaths::class.java)
|
|
||||||
.apply { isAccessible = true }
|
|
||||||
.invoke(null, this@mockk)
|
|
||||||
}
|
}
|
||||||
}.initial()
|
}.initial()
|
||||||
assertTrue(dirFile.exists() && dirFile.isDirectory, "默认初始器未正常初始化【文件夹】.")
|
assertTrue(dirFile.exists() && dirFile.isDirectory, "默认初始器未正常初始化【文件夹】.")
|
||||||
@ -83,10 +84,7 @@ internal class AppPathsTest {
|
|||||||
every { file }.returns(this@apply)
|
every { file }.returns(this@apply)
|
||||||
every { path }.returns(this@apply.canonicalPath)
|
every { path }.returns(this@apply.canonicalPath)
|
||||||
every { initial() }.answers {
|
every { initial() }.answers {
|
||||||
Class.forName("net.lamgc.scalabot.AppConfigsKt")
|
defaultInitializerMethod.invoke(null, this@mockk)
|
||||||
.getDeclaredMethod("defaultInitializer", AppPaths::class.java)
|
|
||||||
.apply { isAccessible = true }
|
|
||||||
.invoke(null, this@mockk)
|
|
||||||
}
|
}
|
||||||
}.initial()
|
}.initial()
|
||||||
assertTrue(this@apply.exists() && this@apply.isFile, "默认初始器未正常初始化【文件】.")
|
assertTrue(this@apply.exists() && this@apply.isFile, "默认初始器未正常初始化【文件】.")
|
||||||
@ -109,16 +107,15 @@ internal class AppPathsTest {
|
|||||||
every { file }.returns(this@apply)
|
every { file }.returns(this@apply)
|
||||||
every { path }.returns(this@apply.canonicalPath)
|
every { path }.returns(this@apply.canonicalPath)
|
||||||
every { initial() }.answers {
|
every { initial() }.answers {
|
||||||
Class.forName("net.lamgc.scalabot.AppConfigsKt")
|
defaultInitializerMethod.invoke(null, this@mockk)
|
||||||
.getDeclaredMethod("defaultInitializer", AppPaths::class.java)
|
|
||||||
.apply { isAccessible = true }
|
|
||||||
.invoke(null, this@mockk)
|
|
||||||
}
|
}
|
||||||
}.initial()
|
}.initial()
|
||||||
verify(exactly = 0) { createNewFile() }
|
verify(exactly = 0) { createNewFile() }
|
||||||
verify(exactly = 0) { mkdir() }
|
verify(exactly = 0) { mkdir() }
|
||||||
verify(exactly = 0) { mkdirs() }
|
verify(exactly = 0) { mkdirs() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defaultInitializerMethod.isAccessible = false
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user