blast0ver
blast0ver

Reputation: 31

Expected at least 1 bean which qualifies as autowire candidate in Test

I'm new in Spring. I am creating a console application using Spring Boot. The application works fine, but when I try to test the Repository, I get an error

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'universityManagerSpringBootApplication': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.universitymanager.springboot.engine.ResponseGenerator' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Please help

ResponseGenerator

@Component
@AllArgsConstructor
public class ResponseGenerator {

    private final DepartmentsRepository departmentsRepository;
    private final LectorsRepository lectorsRepository;
...

Departments

@Entity
@NoArgsConstructor
@Setter
@Getter
@ToString
@Table(name = "departments")
public class Departments {
...

Lectors

@Entity
@NoArgsConstructor
@Setter
@Getter
@ToString
@Table(name = "lectors")
public class Lectors {
...

DepartmentsRepository

@Repository
public interface DepartmentsRepository extends CrudRepository<Departments, Integer> {
...

LectorsRepository

@Repository
public interface LectorsRepository extends CrudRepository<Lectors, Integer> {
...

UniversityManagerSpringBootApplication

@SpringBootApplication
@AllArgsConstructor
public class UniversityManagerSpringBootApplication implements CommandLineRunner {

    private ResponseGenerator responseGenerator;

    public static void main(String[] args) {
        SpringApplication.run(UniversityManagerSpringBootApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
...

DepartmentsRepositoryTest (caused error)

@DataJpaTest
class DepartmentsRepositoryTest {

    @Autowired
    private DepartmentsRepository departmentsRepository;

    @Test
    void injectedComponentIsNotNull(){
        assertNotNull(departmentsRepository);
    }
...

Upvotes: 1

Views: 12029

Answers (3)

blast0ver
blast0ver

Reputation: 31

I found a solution to my problem! This works fine for me https://stackoverflow.com/a/29774201/15349979

Glad if this helps someone

Upvotes: 2

Anil Kumar Athuluri
Anil Kumar Athuluri

Reputation: 653

If you are using TestNG, try to have your test class extends AbstractTestNGSpringContextTests which helps to access spring components in TestNG.

public class DepartmentsRepositoryTest extends AbstractTestNGSpringContextTests

Upvotes: 1

Kaj Hejer
Kaj Hejer

Reputation: 1040

Please try to change @DataJpaTest to @SpringBootTest in your test to make Spring set up at full application context including a ResponseGenerator bean.

The documentation for @DataJpaTest says:

Using this annotation will disable full auto-configuration and instead apply only configuration relevant to JPA tests.

So with this annotation you don't get a ResponseGenerator bean which the stacktrace referes to.

Upvotes: 2

Related Questions