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

Spring boot无法为服务测试类自动连接存储库bean

伯鸿达
2023-03-14

我正在为我的应用程序使用模拟存储库
以下是服务的外观片段:

@Service
public class WeatherStationService {

    @Autowired
    private WeatherStationRepositoryMock weatherRepository;

以下是存储库代码:

@Repository
public class WeatherStationRepositoryMock {

    @Getter
    private List<WeatherStation> stations = new ArrayList<>(
            Arrays.asList(
                    new WeatherStation("huston", "Huston station", RandomGenerator.getRandomGeoInformation()),
                    new WeatherStation("colorado", "Colorado station", RandomGenerator.getRandomGeoInformation())
            )
    );

当我使用@springbootplication执行main()时,它工作正常。

但是,当我想运行测试类:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MockConfig.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
public class WeatherStationServiceTest {

    @Autowired
    @Real
    private WeatherStationService weatherService;

它会因以下stacktrace而失败:

原因:org。springframework。豆。工厂NoSuchBean定义异常:没有类型为“edu”的合格bean。勒勒亚克。存储库。WeatherStationRepositoryMock’可用:至少需要1个符合autowire候选资格的bean。依赖项注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}

以下是MockConfig的内容:

@Configuration
public class MockConfig {
    //**************************** REAL BEANS ******************************
    @Bean
    @Real
    public WeatherStationService weatherServiceReal() {
        return  new WeatherStationService();
    }

Real是用于实际实例的标记注释:

@Retention(RUNTIME)
@Qualifier
public @interface Real {
}

我可以在下一次服务初始化时修复它:

@Service
public class WeatherStationService {
    private WeatherStationRepositoryMock weatherRepository = new WeatherStationRepositoryMock();

它工作正常。

为什么会发生这种情况?
如何修复我的自定义存储库类的自动连接?

共有1个答案

狄旭
2023-03-14

@springbootplication隐式定义了@ComponentScan,它扫描所有子包中的bean。当您使用MockConfig运行测试时,它不会扫描bean。

(1)使用@ComponentScan:

@Configuration
@ComponentScan //Make sure MockConfig is below all beans to discover
public class MockConfig {
    @Bean
    @Real
    public WeatherStationService weatherServiceReal() {
        return  new WeatherStationService();
    }
}

(2) 或者定义所需的bean:

@Configuration
public class MockConfig {
    @Bean
    @Real
    public WeatherStationService weatherServiceReal() {
        return  new WeatherStationService();
    }

    @Bean
    public WeatherStationRepositoryMock weatherStationRepository() {
        return new WeatherStationRepositoryMock()
    }
}
 类似资料:
  • 我正在尝试创建一个Spring Boot测试类,它应该创建Spring上下文,并自动连接服务类以供我测试。 这就是我得到的错误: 原因:org。springframework。豆。工厂NoSuchBeanDefinitionException:没有“com”类型的合格bean。目瞪口呆。戈布斯。基础服务FileImportService'可用:至少需要1个符合autowire候选资格的bean。依

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

  • 我与SpringBoot和JPA合作。我收到一个无法完成的错误。 这是我的主要课程: 这是我的班级失败的原因: 这是类: 这是错误消息: 错误创建bean的名称'请求LoggerImpl':注入自动生成的依赖失败; 无法自动关联字段:专用com。存储库。请求logdao.com。记录器。impl。RequestLoggerImpl。请求logdao;嵌套的异常是org。springframewor

  • 我在Spring Boot上还是新手,我在mongoDB数据库中使用Spring-Boot添加了一个名为文章的文档,我想在该文章中添加注释。但是Spring-boot不能在我的应用程序中自动连接我的存储库。 下面是我的Repository类,它实现了ArticleRepositoryCustom接口,该接口包含一个OuterComment方法。 文章库 这是我的Springboot应用程序课程 当

  • 我有一个使用SpringMVC和SpringBoot的项目,我使用IntelliJ。我的项目如下: 我用注释服务实现。 我用以下内容注释了配置文件 在控制器中,我向服务注入 在测试类中,我使用相同的注释注入相同的服务: 我用以下方法注释测试类: 在控制器中,注入工作正常,但是在测试类中,IntelliJ说: 无法自动连线。找不到WelcomeService类型的beans。 当我运行测试时,它是有

  • 我有一个应用类 我有控制器课 并且,我想为Application test编写一个测试用例,以确保创建的实例类型为HelloController 但是,我在自动连接 hello控制器变量时遇到错误(找不到 hello 控制器类型的 bean)。根据我的理解,@SpringBootTest应该创建上下文并返回一个实例。我们不需要编写任何上下文 xml 或使用任何注释Config 类来获取实例。缺少了