[英]Unit testing repository spring org.springframework.beans.factory.UnsatisfiedDependencyException
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)}
My classes are the following: 我的课程如下:
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); }
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"; }
} }
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.