jeudi 28 novembre 2019

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?

Aucun commentaire:

Enregistrer un commentaire