dimanche 29 mars 2020

Micronaut mock repository interface with Replace annotation

I have a repository implementation of type CrudRepository. Im trying to mock this interface with another interface that extends of it.

I noticed that if I invoke findAll method it works as expected, but when I invoke the findById method I get an error like this:

Micronaut Data method is missing compilation time query information. Ensure that the Micronaut Data annotation processors are declared in your build and try again with a clean re-build.
java.lang.IllegalStateException: Micronaut Data method is missing compilation time query information. Ensure that the Micronaut Data annotation processors are declared in your build and try again with a clean re-build.
    at io.micronaut.data.intercept.DataIntroductionAdvice.intercept(DataIntroductionAdvice.java:97)

This class is class to be mocked

@Repository
interface RepositoryHibernate: CrudRepository<Entity, Long>

This is the class mocked

@Replaces(RepositoryHibernate::class)
abstract class RepositoryCrudMock: RepositoryHibernate {

    val elements = mutableListOf(test1, test2, test3, test4)

    override fun findAll(): MutableIterable<Entity> {
        return elements
    }

    override fun findById(id: Long): Optional<Entity> {
        return when(id) {
            1L -> Optional.of(test1)
            2L -> Optional.of(test2)
            3L -> Optional.of(test3)
            4L -> Optional.of(test4)
            else -> Optional.of(Entity())
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire