比如说,我有以下界面:
interface AppRepository : GraphRepository<App> {
@Query("""MATCH (a:App) RETURN a""")
fun findAll(): List<App>
}
在测试中,我想检查查询字符串的细节,因此我这样做了
open class AppRepositoryTest {
lateinit @Autowired var appRepository: AppRepository
@Test
open fun checkQuery() {
val productionMethod = appRepository.javaClass.getDeclaredMethod("findAll")
val productionQuery = productionMethod!!.getAnnotation(Query::class.java)
//demo test
assertThat(productionQuery!!.value).isNotEmpty() //KotlinNPE
}
}
出于某种我不理解的原因,productionQuery
为 nnull
。我仔细检查了测试类中导入的 Query
和存储库中的 Query
的类型是否相同。
因此,为什么在这种情况下 productionQuery
null
?
最佳答案
您正在从实现类(即 appRepository
实例的类)的 findAll
上加载注释,而不是从 findAll
界面。要改为从 AppRepository
加载注释:
val productionMethod = AppRepository::class.java.getDeclaredMethod("findAll")
val productionQuery = productionMethod!!.getAnnotation(Query::class.java)
关于java - Kotlin 方法上使用的 Java 注释的 getAnnotation 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40220141/