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
@Test
open fun checkQuery() {
val productionMethod = appRepository.javaClass.getDeclaredMethod("findAll")
val productionQuery = productionMethod!!.getAnnotation(Query::class.java)
//demo test
assertThat(productionQuery!!.value).isNotEmpty() //KotlinNPE
}
}
For a reason I don't comprehend, productionQuery
is nnull
. I have double checked that the types of the imported Query
in the test class and the Query
in the repository are the same.
Thus, why is the productionQuery
null
in this case?
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 = AppRepository::class.java.getDeclaredMethod("findAll")
val productionQuery = productionMethod!!.getAnnotation(Query::class.java)