当前位置: 首页 > 知识库问答 >
问题:

Spring启动测试无法自动连接服务类

衡翰翮
2023-03-14

我正在尝试创建一个Spring Boot测试类,它应该创建Spring上下文,并自动连接服务类以供我测试。

这就是我得到的错误:

原因:org。springframework。豆。工厂NoSuchBeanDefinitionException:没有“com”类型的合格bean。目瞪口呆。戈布斯。基础服务FileImportService'可用:至少需要1个符合autowire候选资格的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

文件结构:

测试课程:

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;

Main应用程序类:

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界面:

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.

由以下机构实施:

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());
    }

为什么找不到实施方案?

共有1个答案

琴俊良
2023-03-14

@DataJpaTest注释不会将服务加载到应用程序上下文中。来自Spring文档:https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-功能测试spring boot应用程序测试自动配置的jpa测试

您可以使用@DataJpaTest注释来测试JPA应用程序。默认情况下,它会扫描@Entity类并配置Spring数据JPA存储库。如果类路径上有嵌入式数据库,它也会配置一个。常规@Component bean不会加载到ApplicationContext中。

您可以使用@SpringBootTest注释来代替DataJpaTest。希望有帮助!

 类似资料:
  • 我已经用@Autowired注释为相应的存储库定义了服务类 存储库接口定义为从JpaReepository扩展 应用程序自动编译服务类 运行时,我得到以下错误

  • 我正在为我的应用程序使用模拟存储库 以下是服务的外观片段: 以下是存储库代码: 当我使用执行main()时,它工作正常。 但是,当我想运行测试类: 它会因以下stacktrace而失败: 原因:org。springframework。豆。工厂NoSuchBean定义异常:没有类型为“edu”的合格bean。勒勒亚克。存储库。WeatherStationRepositoryMock’可用:至少需要1

  • 我创建了使用jdbc处理DB的Dao存储库。 我在我的服务类中自动连接了这个存储库。 然后我尝试在测试类中自动连接我的服务类。 当我开始测试时,我得到下一个错误: 服务中构造函数的参数0需要找不到DaoImpl类型的bean。 我如何解决我的问题?

  • 我想尝试嵌入式数据库测试我的DAO对象在spring应用程序。 在应用程序上下文中,我有以下标记: 我的JUnit测试类需要使用这个bean: 一切正常(创建了“DataSourceEmbedded”bean),但当我试图在PartnerDAOTest类中自动调用它们时,spring抛出了以下异常: testSavePartner(Sandbox.PartnerDaoTest):创建名为“Sand

  • 我有以下课程: 应用和配置类 MyRestController类 我的实用类 当我启动应用程序并将其作为独立jar运行时,或者从IDE(Eclipse)运行时,没有任何问题,一切都按预期进行。 然而,我想写一个单元测试来测试我的MyRestController类。。。我得到了一个NPE,因为自动连接字段util为null(在MyRestController类中)。 这是我的测试课: 我肯定错过了一

  • 我能够运行一个Rest Controller PUT方法,该方法通过Spring Boot Application使用预计的自动更新@Service。在尝试执行Spring JUnit测试时,相同的自动配线失败。我曾尝试阅读多个线程与类似的问题。我确保我没有通过“新”关键字创建@服务,我尝试了上下文配置和其他方法...但是一切似乎都是徒劳的。我不确定我哪里出错了。 我的Spring Boot应用程