1

Sprich, ich habe die folgende Schnittstelle:getAnnotation für Java Annotations auf Kotlin Methode gibt null zurück

interface AppRepository : GraphRepository<App> { 

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

In einem Test Ich möchte Besonderheiten der Query-String überprüfen und deshalb muss ich

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

Aus einem Grund, den ich nicht verstehe, productionQuery ist n null. Ich habe überprüft, dass die Typen der importierten Query in der Testklasse und die Query im Repository identisch sind.

Also, warum ist die productionQuerynull in diesem Fall?

Antwort

4

Sie laden Annotationen auf findAll von der implementierenden Klasse (d. H. Die Klasse der appRepository Instanz), nicht auf findAll von der Schnittstelle. Um Anmerkungen von AppRepository statt zu laden:

val productionMethod = AppRepository::class.java.getDeclaredMethod("findAll") 
val productionQuery = productionMethod!!.getAnnotation(Query::class.java) 
+0

verdammt :) so peinlich. –

Verwandte Themen