测试KTOR时,我有问题。基本上Application -Test类未注入存储库类,它显示了Java的错误.. ClassCastExce
这是我的应用程序测试课程,
class ApplicationTest {
private val heroRepository: HeroRepository by inject(HeroRepository::class.java)
@OptIn(InternalAPI::class)
@Test
fun `access all heroes endpoints, assert correct information`() = testApplication {
val response = client.get("/naruto/heroes")
assertEquals(
expected =
"""
{
success = true,
message = "ok",
prevPage = null,
nextPage = 2,
heroes = ${heroRepository.heroes[1]!!}
}
""".trimIndent() ,
actual = response.bodyAsText()
)
}
}
它显示了java.lang.classcastException的错误时,当HerorePository被注入时,我正在使用Koin进行依赖注入
java.lang.ClassCastException: class com.example.repository.HeroRepositoryImpl cannot be cast to class com.example.repository.HeroRepository (com.example.repository.HeroRepositoryImpl is in unnamed module of loader io.ktor.server.engine.OverridingClassLoader$ChildURLClassLoader @7f6ad6c8; com.example.repository.HeroRepository is in unnamed module of loader 'app')
,这是我的AllheroeSroute
fun Route.getAllHeroes() {
val heroRepository: HeroRepository by inject()
get("/naruto/heroes") {
try {
val page = call.request.queryParameters["page"]?.toInt() ?: 1
require(page in 1..5)
val apiResponse = heroRepository.getAllHeroes(page = page)
call.respond(
message = apiResponse,
status = HttpStatusCode.OK
)
} catch (e: NumberFormatException) {
call.respond(
message = ApiResponse(success = false, message = "Only numbers allowed"),
status = HttpStatusCode.BadRequest
)
} catch (e: IllegalArgumentException) {
call.respond(
message = ApiResponse(success = false, message = "Heroes Not Found"),
status = HttpStatusCode.BadRequest
)
}
}
}
This is my application test class
class ApplicationTest {
private val heroRepository: HeroRepository by inject(HeroRepository::class.java)
@OptIn(InternalAPI::class)
@Test
fun `access all heroes endpoints, assert correct information`() = testApplication {
val response = client.get("/naruto/heroes")
assertEquals(
expected =
"""
{
success = true,
message = "ok",
prevPage = null,
nextPage = 2,
heroes = ${heroRepository.heroes[1]!!}
}
""".trimIndent() ,
actual = response.bodyAsText()
)
}
}
It show the error of java.lang.ClassCastException when heroRepository is getting inject and i am using koin for dependency injection
java.lang.ClassCastException: class com.example.repository.HeroRepositoryImpl cannot be cast to class com.example.repository.HeroRepository (com.example.repository.HeroRepositoryImpl is in unnamed module of loader io.ktor.server.engine.OverridingClassLoader$ChildURLClassLoader @7f6ad6c8; com.example.repository.HeroRepository is in unnamed module of loader 'app')
And this is my AllHeroesRoute and here it's perfectly injecting heroRepository
fun Route.getAllHeroes() {
val heroRepository: HeroRepository by inject()
get("/naruto/heroes") {
try {
val page = call.request.queryParameters["page"]?.toInt() ?: 1
require(page in 1..5)
val apiResponse = heroRepository.getAllHeroes(page = page)
call.respond(
message = apiResponse,
status = HttpStatusCode.OK
)
} catch (e: NumberFormatException) {
call.respond(
message = ApiResponse(success = false, message = "Only numbers allowed"),
status = HttpStatusCode.BadRequest
)
} catch (e: IllegalArgumentException) {
call.respond(
message = ApiResponse(success = false, message = "Heroes Not Found"),
status = HttpStatusCode.BadRequest
)
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题,禁用了开发模式修复了它:
I had the same issue, disabling the developmentMode fixed it: