crmepham
crmepham

Reputation: 4740

Spring boot test unable to autowire service class

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?

Upvotes: 1

Views: 5690

Answers (1)

Tomasz Nowak
Tomasz Nowak

Reputation: 251

@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!

Upvotes: 5

Related Questions