[英]No qualifying bean for autowired JPA repository in JUnit test in Spring Boot application
[英]"No qualifying bean" for repository in Groovy Spring JPA application
我知道关于这个主题有很多类似的问题,但是到目前为止我发现的所有问题都不是我的情况,或者参考我所阅读的以下解决方案之一,这些解决方案要么已过时,要么不适用:
错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean
of type 'com.package.repository.ChannelBalanceAdjustmentRepository' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
还有另一个错误信息:
CashReinvestDividendSpec > test FAILED
java.lang.IllegalStateException at DefaultCacheAwareContextLoaderDelegate.java:132
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at AutowiredAnnotationBeanPostProcessor.java:659
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at AutowiredAnnotationBeanPostProcessor.java:659
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException at DefaultListableBeanFactory.java:1790
存储库:
package com.package.repository
import com.package.domain.Channel
import com.package.domain.ChannelBalanceAdjustment
import org.springframework.data.repository.CrudRepository
import java.time.LocalDate
interface ChannelBalanceAdjustmentRepository extends CrudRepository<ChannelBalanceAdjustment, Long>{
ChannelBalanceAdjustment findByTypeAndChannelAndValueDate(ChannelBalanceAdjustment.Type type, Channel channel, LocalDate valueDate)
}
应用 class:
package com.package
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
@SpringBootApplication
class ProcessPendingDividendsApplication {
static void main(String[] args) {
SpringApplication.run(ProcessPendingDividendsApplication, args)
}
}
使用存储库的摘录:
package com.package.service
// imports
@Service
@Slf4j
class ProcessingPendingDividendsService {
@Autowired
ChannelBalanceAdjustmentRepository channelBalanceAdjustmentRepository
它用于我正在运行的 Spock 测试:
@SpringBootTest
@Transactional
@AutoConfigureTestDatabase
@AutoConfigureTestEntityManager
class CashReinvestDividendSpec extends Specification{
@Autowired
CashReinvestDividendRepository cashReinvestDividendRepository
@Autowired
TestEntityManager entityManager
def "test" () {
我可以添加任何其他需要的东西,但不想用不相关的东西把问题搞得一团糟。
仔细检查您的实体。 我以前见过 jpa 存储库必须引用正确配置的实体,如果实体 object 缺少 @Entity 或其中一列配置不正确,则会引发红鲱鱼错误。
nasch 你在分享实际的代码/错误片段吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.