Spring boot test unable to autowire service class

crmepham :

I am attempting to create a Spring Boot test class which should create the Spring context and autowire the service class for me to test.

This is the error I am getting:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.gobsmack.gobs.base.service.FileImportService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

The file structue:

enter image description here

The Test class:

package com.example.gobs.base.service;

import com.example.gobs.base.entity.FileImportEntity;
import com.example.gobs.base.enums.FileImportType;
import lombok.val;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@DataJpaTest
@RunWith(SpringRunner.class)
public class FileImportServiceTest {

    @Autowired
    private FileImportService fileImportService;

    private FileImportEntity entity;

The Main application class:

package com.example.gobs.base;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Used only for testing.
 */
@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

FileImportService interface:

package com.example.gobs.base.service;

import com.example.gobs.base.entity.FileImportEntity;
import com.example.gobs.base.enums.FileImportType;

import java.util.List;

public interface FileImportService {

    /**
     * List all {@link FileImportEntity}s.

Which is implemented by:

package com.example.gobs.base.service.impl;

import com.example.gobs.base.entity.FileImportEntity;
import com.example.gobs.base.enums.FileImportType;
import com.example.gobs.base.repository.FileImportRepository;
import com.example.gobs.base.service.FileImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class FileImportServiceImpl implements FileImportService {

    @Autowired
    private FileImportRepository repository;

    @Override
    public List<FileImportEntity> listAllFileImportsByType(FileImportType type) {
        return repository.findAllByType(type.name());
    }

Why can it not find the implementation?

Tomasz Nowak :

@DataJpaTest annotation doesn't make services loaded to the application context. From Spring documentation: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test

You can use the @DataJpaTest annotation to test JPA applications. By default, it scans for @Entity classes and configures Spring Data JPA repositories. If an embedded database is available on the classpath, it configures one as well. Regular @Component beans are not loaded into the ApplicationContext.

You could use @SpringBootTest annotation instead of DataJpaTest. Hope that helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can't Autowire JpaRepository in Junit test - Spring boot application

Unable to autowire the service inside my authentication filter in Spring

Spring Boot can't autowire map bean in @configuration class

Can't Autowire Service in JUnit 4 Test in Spring Boot Multi Module Application

How to autowire a bean in other class in a Spring Boot application?

Spring Boot autowire field in singleton service/controller by request scoped bean

How to autowire Spring service with Class name?

Spring Boot not autowiring class in test

Spring boot test: Unable to instantiate inner configuration class

Autowire a Spring service in JUnit test

Spring Boot, @Autowire into an unmanaged class using @Configurable and load time weaving

How to Autowire a Spring-annotated service class in a @Configuration class?

How to only autowire a specific class in JUnit spring test?

Unable to find a SpringBootConfiguration in Spring Boot Test 1.4

Junit Test in Spring Boot does not inject the service

Spring boot field injection with autowire not working in JUnit test

How autowire a repository in an integration test with Spring Boot?

spring boot not able to inject spring service in spring test class

Unable to inject a @Service in a Spring Boot Test

Spring-Boot Cloug Config Client unable to Autowire

How @Autowire works in spring-boot unit test?

Spring-boot, unable to autowire a class.No default constructor found Exception is raised

Unable to test the REST API developed with Spring Boot

Spring boot - Service class is null

spring-boot: test @service class

Spring-boot & multiple database connections: autowire service does not work

Unable to run Spring Boot simple REST service

Spring Boot service layer test with SpringMockk

I can't autowire Service class in Spring Boot Test