I know there are a bunch of similar questions on this topic, however all of them that I’ve found so far are either not quite my situation, or refer to one of the following solutions that from what I’ve read is either outdated or inapplicable:
- @EnableJpaRepositories annotation – taken care of by @SpringBootApplication
- @Repository annotation – not needed when extending Repository interface
The error:
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)}
And another error message:
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
The repository:
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) }
The application 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) } }
Excerpt from where the repository is used:
package com.package.service // imports @Service @Slf4j class ProcessingPendingDividendsService { @Autowired ChannelBalanceAdjustmentRepository channelBalanceAdjustmentRepository
And it is used in the Spock test I’m running:
@SpringBootTest @Transactional @AutoConfigureTestDatabase @AutoConfigureTestEntityManager class CashReinvestDividendSpec extends Specification{ @Autowired CashReinvestDividendRepository cashReinvestDividendRepository @Autowired TestEntityManager entityManager def "test" () {
I can add anything else needed but don’t want to clutter up the question with irrelevant stuff.
Advertisement
Answer
Double-check your entity. I’ve seen this before where the jpa repository has to reference a correctly configured entity, and if the entity object is missing @Entity or one of the columns is not configured correctly, it throws a red herring error.