Christoph Möbius Christoph Möbius - 1 year ago 144
Java Question

getAnnotation for Java annotation used on Kotlin method returns null

Say, I have the following interface:

interface AppRepository : GraphRepository<App> {

@Query("""MATCH (a:App) RETURN a""")
fun findAll(): List<App>

In a test I want to check specifics of the query string and therefore I do

open class AppRepositoryTest {

lateinit @Autowired var appRepository: AppRepository

open fun checkQuery() {
val productionMethod = appRepository.javaClass.getDeclaredMethod("findAll")
val productionQuery = productionMethod!!.getAnnotation(

//demo test
assertThat(productionQuery!!.value).isNotEmpty() //KotlinNPE

For a reason I don't comprehend,
is n
. I have double checked that the types of the imported
in the test class and the
in the repository are the same.

Thus, why is the
in this case?

Answer Source

You're loading annotations on findAll from the implementing class (i.e. the class of the appRepository instance), not on findAll from the interface. To load annotations from AppRepository instead:

val productionMethod ="findAll")
val productionQuery = productionMethod!!.getAnnotation(
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download