【问题标题】:Why @MockBean ConversionService causes an error为什么@MockBean ConversionService 会导致错误
【发布时间】:2020-07-16 13:30:24
【问题描述】:

假设:使用 Intellij IDEA Ultimate 创建的 JUnit 5 和空 SpringBoot 项目。对于包含 @Autowired ConversionService 的控制器的项目,我得到了相同的结果。

我需要在测试中使用 ConversionService 的模拟版本。

这是我的方法:

import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.convert.ConversionService;

@SpringBootTest
public class ConversionServiceTest {

    @MockBean
    ConversionService conversionService;

    @Test
    void test() {}
}

以上代码导致错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.format.support.FormattingConversionService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value="mvcConversionService")}

这个错误的原因是什么,解决方法是什么?

【问题讨论】:

    标签: spring-boot junit mocking


    【解决方案1】:

    解决方案是将@MockBean 替换为@SpyBean。

    【讨论】:

      猜你喜欢
      • 2012-05-04
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      相关资源
      最近更新 更多