首页 > 解决方案 > 带有替换注释的 Micronaut 模拟存储库接口

问题描述

我有一个CrudRepository类型的存储库实现。我试图用另一个扩展它的接口来模拟这个接口。

我注意到,如果我调用 findAll 方法,它会按预期工作,但是当我调用 findById 方法时,我会收到如下错误:

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)

这个类是要模拟的类

@Repository
interface RepositoryHibernate: CrudRepository<Entity, Long>

这是被嘲笑的班级

@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())
        }
    }
}

标签: unit-testingtestingkotlinmicronautmicronaut-data

解决方案


推荐阅读