gpt4 book ai didi

java - 单元测试存储库 spring org.springframework.beans.factory.UnsatisfiedDependencyException

转载 作者:可可西里 更新时间:2023-11-01 10:45:24 24 4
gpt4 key购买 nike

  1. 我正在使用 Spring Boot 和 MongoDB 创建一个简单的学校应用程序。我想测试服务类中定义的方法,但出现以下异常:

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name'com.backintime.BackInTimeSpring.Service.TeacherServiceTest': Unsatisfied dependency expressed through field 'teacherService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying     bean of type 'com.backintime.BackInTimeSpring.Service.TeacherService' available:     expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:    {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  2. 我的类(class)如下:

  3. 存储库:

    package com.backintime.BackInTimeSpring.Model.Repository;
    @Component
    @Repository
    public interface ITeacherRepository extends MongoRepository<Teacher,String> {

    @Query("{}")
    Stream<Teacher> findAllTeachers();

    List<Teacher> findByLastNameIgnoreCase(String lastName);
    List<Teacher> findByFirstNameIgnoreCase(String firstName);

    }
  4. 服务:

    package com.backintime.BackInTimeSpring.Service;
    @Component
    @Service
    public class TeacherService {
    @Autowired
    private ITeacherRepository teacherRepository;

    public List<Teacher> retrieveAllTeachers(){
    return this.teacherRepository.findAllTeachers().sorted(Comparator.comparing(Teacher::getLastName)).collect(Collectors.toList());
    }
    public Teacher retrieveTeachersById(String id) {
    return this.teacherRepository.findAllTeachers().filter(t -> t.getId().equals(id)).findFirst().get();
    }
    public List<Teacher> retrieveTeachersByLastName(String lastName){
    return this.teacherRepository.findByLastNameIgnoreCase(lastName);
    }
    public List<Teacher> retrieveTeachersByFirstName(String firstName){
    return this.teacherRepository.findByFirstNameIgnoreCase(firstName);
    }
    public void saveTeacher(Teacher t){
    this.teacherRepository.save(t);
    }

    public String greet() {
    return "Hello World";
    }

  5. 单元测试:

    package com.backintime.BackInTimeSpring.Service;
    @RunWith(SpringRunner.class)
    @ComponentScan(basePackages = "com.backintime.BackInTimeSpring")
    public class TeacherServiceTest {

    @Autowired
    TeacherService teacherService;
    @Test
    public void retrieveAllTeachers() {
    assertEquals(3, teacherService.retrieveAllTeachers().size());
    }

最佳答案

发生这种情况是因为您在错误的地方使用了 @ComponentScan

通常它用在您的主应用程序类或配置类中,而不是您定义 bean 的地方。

请引用此链接:https://springframework.guru/spring-component-scan/

这里有一个类似的问题,正如您所看到的,他们也在主应用程序中使用了 @ComponentScan

How to use @ComponentScan together with test-specific ContextConfigurations in SpringJunit4TestRunner?

关于java - 单元测试存储库 spring org.springframework.beans.factory.UnsatisfiedDependencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53721342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com