Spring Boot测试报错:无法创建UtentiRepositoryTest Bean问题求助
错误信息
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name
'com.example.demo.RepositoryTest.UtentiRepositoryTest': Unsatisfied
dependency expressed through field 'utentiRepository': No qualifying
bean of type 'Repository.UtentiRepository' available: expected at
least 1 bean which qualifies as autowire candidate. Dependency
annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
问题分析
这个错误的核心是Spring容器无法找到UtentiRepository类型的Bean,导致自动注入失败,主要由以下几个问题引发:
- 包扫描范围不匹配:Spring Boot默认仅扫描启动类所在包及其子包,
Repository、Entity等包不在默认扫描范围内 - 冗余注解冲突:测试类和Service实现类同时使用了多个Bean声明注解,可能干扰Spring的Bean注册逻辑
- Repository未被正确识别:缺少JPA相关的扫描配置,导致Spring Data JPA无法生成Repository代理类
修复方案
1. 调整包扫描配置
如果不想修改现有包结构,在Spring Boot启动类上添加扫描注解,明确指定需要扫描的包:
@SpringBootApplication @ComponentScan(basePackages = {"com.example.demo", "Repository", "Entity", "Service", "ServiceImp"}) @EnableJpaRepositories(basePackages = "Repository") // 指定JPA Repository扫描包 @EntityScan(basePackages = "Entity") // 指定实体类扫描包 public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
或者更规范的做法是将所有业务包移动到com.example.demo的子包下(如com.example.demo.repository、com.example.demo.entity),这样Spring Boot会自动扫描所有子包,无需额外配置。
2. 清理测试类冗余注解
测试类UtentiRepositoryTest仅保留@SpringBootTest即可,@Component属于多余注解,会干扰测试类的容器注册:
package com.example.demo.RepositoryTest; import Entity.Utenti; import Repository.UtentiRepository; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest public class UtentiRepositoryTest { @Autowired UtentiRepository utentiRepository; @Test public void testSalvaUtente(){ // Arrage Utenti utenti=Utenti.builder().nome("Mario").cognome("Scano").email("mario@mail.com").pwd("password").id(4L).build(); Utenti utenti2=Utenti.builder().nome("Mario").cognome("Scano").email("mario@mail.com").pwd("password").id(3L).build(); // Act utentiRepository.save(utenti); utentiRepository.save(utenti2); List<Utenti> lista=utentiRepository.findAll(); // Assert Assertions.assertThat(lista).isNotNull(); Assertions.assertThat(lista.size()).isEqualTo(2); } }
3. 优化Service实现类注解
UtentiServiceImp仅保留@Service注解即可,同时推荐使用构造器注入(可移除字段上的@Autowired,保持代码简洁):
package ServiceImp; import DTO.UtentiDto; import Entity.Utenti; import Repository.UtentiRepository; import Service.UtentiService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UtentiServiceImp implements UtentiService { private final UtentiRepository utentiRepository; @Autowired public UtentiServiceImp(UtentiRepository utentiRepository) { this.utentiRepository = utentiRepository; } @Override public UtentiDto salvaUtente(UtentiDto utentiDto){ Utenti user=new Utenti(); user.setId(utentiDto.getId()); user.setNome(utentiDto.getNome()); user.setCognome(utentiDto.getCognome()); user.setEmail(utentiDto.getEmail()); user.setPwd(utentiDto.getPwd()); Utenti utenti=utentiRepository.save(user); UtentiDto utentiResponse=new UtentiDto(); utentiResponse.setId(utenti.getId()); utentiResponse.setNome(utenti.getNome()); utentiResponse.setCognome(utenti.getCognome()); utentiResponse.setEmail(utenti.getEmail()); utentiResponse.setPwd(utenti.getPwd()); return utentiResponse; } }
内容的提问来源于stack exchange,提问作者FrancoSasso

