简体   繁体   English

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

[英]Unit testing repository spring org.springframework.beans.factory.UnsatisfiedDependencyException

  1. I'm using Spring Boot and MongoDB to create a simple school application. 我正在使用Spring Boot和MongoDB创建一个简单的学校应用程序。 I want to test the methods that are defined in the service class but I get the following exception: 我想测试服务类中定义的方法,但出现以下异常:

     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. My classes are the following: 我的课程如下:

  3. Repository: 库:

     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. Service: 服务:

     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. Unittest: 单元测试:

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

This is happening due to the fact that you are using @ComponentScan in the wrong place. 这是由于您在错误的位置使用@ComponentScan而导致的。

Usually it is used in your Main Application class or in configuration classes not where you define the beans. 通常在主应用程序类或配置类中而不是在定义Bean的地方使用它。

Please refer to this link: https://springframework.guru/spring-component-scan/ 请参考此链接: https : //springframework.guru/spring-component-scan/

There is a similar question in here, and as you can see they are as well using @ComponentScan in the Main application 这里有一个类似的问题,您可以在Main应用程序中使用@ComponentScan看到它们

How to use @ComponentScan together with test-specific ContextConfigurations in SpringJunit4TestRunner? 如何在SpringJunit4TestRunner中将@ComponentScan与测试特定的ContextConfiguration一起使用?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Data JPA:org.springframework.beans.factory.UnsatisfiedDependencyException: - Spring Data JPA: org.springframework.beans.factory.UnsatisfiedDependencyException: 构建项目spring org.springframework.beans.factory.UnsatisfiedDependencyException时出错 - Error building project spring org.springframework.beans.factory.UnsatisfiedDependencyException Spring Boot:org.springframework.beans.factory.UnsatisfiedDependencyException - Spring Boot:org.springframework.beans.factory.UnsatisfiedDependencyException Spring Boot错误org.springframework.beans.factory.UnsatisfiedDependencyException - Spring Boot Error org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factory.UnsatisfiedDependencyException 1 - org.springframework.beans.factory.UnsatisfiedDependencyException 1 SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException - SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factory.UnsatisfiedDependencyException Springboot - org.springframework.beans.factory.UnsatisfiedDependencyException Springboot org.springframework.beans.factory.UnsatisfiedDependencyException-是错误 - org.springframework.beans.factory.UnsatisfiedDependencyException - is the Error 异常org.springframework.beans.factory.UnsatisfiedDependencyException - Exception org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factory.UnsatisfiedDependencyException问题: - Issue with org.springframework.beans.factory.UnsatisfiedDependencyException:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM