Wat Als
Wat Als

Reputation: 33

Unit testing repository spring org.springframework.beans.factory.UnsatisfiedDependencyException

  1. I'm using Spring Boot and MongoDB to create a simple school application. 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());
    }
    

Upvotes: 2

Views: 311

Answers (1)

OEH
OEH

Reputation: 705

This is happening due to the fact that you are using @ComponentScan in the wrong place.

Usually it is used in your Main Application class or in configuration classes not where you define the beans.

Please refer to this link: 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

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

Upvotes: 1

Related Questions