测试单元Spring boot:无法注册模拟bean

2024-02-19

我有两类测试:1-用于控制器类的单元测试,2-用于服务类的单元测试,如下所示:

1- 测试类控制器:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class CentroDeCustoRestControllerTeste {

    @Autowired
    private MockMvc mvc;

    @MockBean
    private CentroDeCustoService service;

    @Test
    @WithMockUser(username = "[email protected] /cdn-cgi/l/email-protection", authorities = {"ADMIN"})
    public void aoBuscarUmCentroDeCustoRetornarUmJsonComPropriedadeCentroDeCusto() throws Exception {
        CentroDeCusto centroDeCusto = new CentroDeCusto();
        centroDeCusto.setNome("Financeiro");

        given(service.centroDeCusto(1L)).willReturn(Optional.of(centroDeCusto));

        mvc.perform(get("/centrosdecustos/1").contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.centroDeCusto", org.hamcrest.core.Is.is(centroDeCusto.getCentroDeCusto())));
        }
     }

2- 测试类服务:

@RunWith(SpringRunner.class)
public class CentroDeCustoServiceImplTeste {

    @TestConfiguration
    static class CentroDeCustoServiceImplConfiguracaoContexto {
        @Bean
        public CentroDeCustoService centroDeCustoService() {
            return new CentroDeCustoServiceImpl();
        }
    }

    @Autowired
    private CentroDeCustoService service;

    @MockBean
    private CentroDeCustoRepository repository;

    @Before
    public void setup() {
        CentroDeCusto centroDeCusto = new CentroDeCusto();
        centroDeCusto.setId(1L);
        Optional<CentroDeCusto> centroDeCustoOpt = Optional.of(centroDeCusto);

        Mockito.when(repository.findById(1L)).thenReturn(centroDeCustoOpt);
    }

    @Test
    public void aoBuscarUmCentroDeCustoExistenteRetornarUmCentroDeCustoOptional() {
        Optional<CentroDeCusto> resultado = service.centroDeCusto(1L);
        CentroDeCusto centroDeCusto = resultado.get();

        Assertions.assertThat(centroDeCusto.getId())
                .isEqualTo(1L);
    }
}

接口 CentroDeCustoService

public interface CentroDeCustoService {

    Optional<CentroDeCusto> centroDeCusto(Long id);

    Page<CentroDeCusto> centrosDeCusto(Pageable pagina);

    Optional<CentroDeCusto> buscaCentroDeCustoPeloNumero(String numero);

    List<CentroDeCusto> buscaCentrosDeCustoDeUmaContaGerencial(Long idDaContaGerencial);

    boolean verificaSeCentroDeCustoJahExiste(CentroDeCusto centroDeCusto);

    CentroDeCusto criaCentroDeCusto(CentroDeCusto centroDeCusto);

    CentroDeCusto atualizaCentroDeCusto(CentroDeCusto centroDeCusto);

    boolean delataCentroDeCusto(Long id);
}

类 CentroDeCustoServiceImpl

@Service
public class CentroDeCustoServiceImpl implements CentroDeCustoService {
    @Autowired
    private CentroDeCustoRepository repository;

    @Override
    public Optional<CentroDeCusto> centroDeCusto(Long id) {
        return repository.findById(id);
    }

    @Override
    public Page<CentroDeCusto> centrosDeCusto(Pageable pagina) {
        return repository.departamentosPorDataDeAtualizacao(pagina);
    }

    @Override
    public Optional<CentroDeCusto> buscaCentroDeCustoPeloNumero(String numero) {
        return repository.findByCentroDeCusto(numero);
    }

    @Override
    public List<CentroDeCusto> buscaCentrosDeCustoDeUmaContaGerencial(Long idDaContaGerencial) {
        return repository.findByContasGerenciaisId(idDaContaGerencial);
    }

    @Override
    public boolean verificaSeCentroDeCustoJahExiste(CentroDeCusto centroDeCusto) {
        String numeroDoCentroDeCusto = centroDeCusto.getCentroDeCusto();
        Long idDoCentroDeCusto = centroDeCusto.getId();
        Optional<CentroDeCusto> centroDeCustoOpt = repository.findByCentroDeCustoAndIdNotIn(numeroDoCentroDeCusto, idDoCentroDeCusto);

        if (centroDeCustoOpt.isPresent())
            return true;

        return false;
    }

    @Override
    public CentroDeCusto criaCentroDeCusto(CentroDeCusto centroDeCusto) {
        return repository.save(centroDeCusto);
    }

    @Override
    public CentroDeCusto atualizaCentroDeCusto(CentroDeCusto centroDeCusto) {
        return repository.save(centroDeCusto);
    }

    @Override
    public boolean delataCentroDeCusto(Long id) {
        Optional<CentroDeCusto> centroDeCustoOpt = centroDeCusto(id);
        if (centroDeCustoOpt.isPresent()) {
            repository.delete(centroDeCustoOpt.get());
            return true;
        }

        return false;
    }
}

当我运行控制器时,为 Service 类创建的测试似乎会产生影响,因为这两个测试都调用 CentroDeCustoService 对象。如何正确配置测试类以避免更多冲突?

Error: 无法注册模拟 bean br.com.teste.reembolso.centrocusto.CentroDeCustoService 需要一个匹配的 bean 来替换,但找到了 [centroDeCustoService、centroDeCustoServiceImpl]:

java.lang.IllegalStateException: Failed to load ApplicationContext

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalStateException: Unable to register mock bean br.com.teste.reembolso.centrocusto.CentroDeCustoService expected a single matching bean to replace but found [centroDeCustoService, centroDeCustoServiceImpl]
    at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.getBeanName(MockitoPostProcessor.java:239)
    at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.registerMock(MockitoPostProcessor.java:184)
    at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.register(MockitoPostProcessor.java:174)
    at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.postProcessBeanFactory(MockitoPostProcessor.java:144)
    at org.springframework.boot.test.mock.mockito.MockitoPostProcessor.postProcessBeanFactory(MockitoPostProcessor.java:131)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:282)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:170)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:694)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:139)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
    ... 24 more

看起来很矛盾,但我不知道如何解决。有谁能够帮助我?


除了以下之外,您还可以使用限定符@MockBean如果您有多个相同类型的 bean。

 @MockBean
 @Qualifier("centroDeCustoServiceImpl"
 private CentroDeCustoService service;

请参阅此处的 MockBean java 文档:

https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/mock/mockito/MockBean.html https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/mock/mockito/MockBean.html

另外,根据对您问题的第一条评论,您可以避免在CentroDeCustoServiceImplTeste单元测试。您可以使用 MockitoJunitRunner

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

测试单元Spring boot:无法注册模拟bean 的相关文章

随机推荐

  • 动态添加到horizo​​ntalscrollview

    我遵循了一些在线教程 向您展示如何使用多个 xml 文件创建静态水平滚动视图 然而 我希望能够从数据库中获取内容 用内容填充一个新视图 来自库存 xml 布局 然后将其添加到水平滚动视图中 是否有任何教程向您展示如何将动态视图添加到水平滚动
  • Three.js 将对象附加到骨骼

    有什么方法可以将网格连接到骨骼上吗 例如 我加载动画 js 角色 并且想将武器附加到它的手上 可以对 Bone 和 Object3D 原型进行一些简单的修改 由于骨骼继承自 Object3D 因此它们可能有子级 因此我们可以轻松地将网格添加
  • 是否可以通过编程方式运行 Simulink 模型并测量其状态?

    我希望为现有 Simulink 模型设置一个测试集 理想情况下 我可以完全控制模型 明确地步进并测量模型中任何总线上任何信号的状态 正如可能已经收集到的 这是该模型的单元测试系统的前身 因此 我不能真正证明更改模型以适应测试是合理的 测试必
  • 放大镜显示 UIWindow 后面

    我们有一个带有汉堡菜单导航的应用程序 菜单本身是一个位于键窗口后面的窗口 当用户长按一个uitextfield并调出放大镜时 放大镜会显示后面UIWindow的内容 有没有人有任何想法 Video https youtu be CvlLaF
  • fileUpload 在使用 JSF 2.2 的 PrimeFaces 3.5 中不会触发事件

    我无法在 PrimeFaces 3 5 上使用 fileUpload 组件来触发该事件 我读过很多关于该主题的帖子并遵循了那里的建议 但仍然不起作用 我尝试了所有模式 简单 自动 高级 但没有成功 如果我使用 JSF 规范中的标准 inpu
  • 实体框架最佳实践:哪一层应该调用 SaveChanges()?

    对于一个干净的数据模型 我会来回讨论这个 以审批工作流程为例 假设在我的 Web 应用程序中 我有一个页面 可让用户标记MyEntityObject需要批准 MyEntityObject有一些控制其审批工作流程的属性 因此我有一个通用的实用
  • swift 中的 C 数组内存释放

    var cpuInfo processor info array t nil var numCpuInfo mach msg type number t 0 var coresTotalUsage Float 0 0 var numCPUs
  • 缺少对 Nexus 的 PUT 的请求实体响应

    我正在尝试模拟发布到我的 Nexus 存储库的 Maven 工件 试图了解为什么我的 gradle 构建失败 I try curl u me secret X PUT T my artifact H Content Type maven a
  • 多集群并行方法中启动时的变量范围

    我正在尝试弄清楚如何将函数和包传递给boot 运行并行计算时的函数 在循环内加载包或定义函数似乎非常昂贵 这foreach 我经常用于其他并行任务的函数有一个 packages 和 export 参数来处理这个问题 请参阅此所以问题 htt
  • gpg:未找到有效的 OpenPGP 数据

    我正在尝试在 Ubuntu 13 10 上安装 Jenkins 当我尝试运行以下命令时 出现上述错误 wget q O http pkg jenkins ci org debian jenkins ci org key sudo apt k
  • 日期之间的月份差异[重复]

    这个问题在这里已经有答案了 可能的重复 月份差异 https stackoverflow com questions 1525990 difference in months Hi all 我们如何使用 LINQ 计算两个日期之间的月份差异
  • 如何以编程方式将网站添加到 Internet Explorer 11 兼容性列表?

    我尝试在以下位置添加注册表项 Hive HKEY CURRENT USER Key Path Software Policies Microsoft Internet Explorer BrowserEmulation PolicyList
  • 使用 jquery 获取 html5 的值

    前几天 我在 stackoverflow 中阅读答案 我读到 jquery 可以获取 html5 的值
  • memmove 实际上是否“移动”了一块内存并在源头留下了零? [复制]

    这个问题在这里已经有答案了 可能的重复 memcpy 与 memmove https stackoverflow com questions 4415910 memcpy vs memmove Does memmove http www c
  • github 存储库中的自定义语言

    Git 显示构成存储库的语言的百分比 然而 对于我的一个项目 我想使用我自己的自定义语言 我知道我可以创建一个 gitattributes 文件并放置 py linguist language Python使所有 py 文件被识别为 Pyt
  • 导入错误:没有名为“flask.ext”的模块[重复]

    这个问题在这里已经有答案了 当我像这样导入 Flask 扩展时 它工作正常 from flask module import Module 这样扩展就安装正确了 但每当我尝试像这样导入 Flask 扩展时 from flask ext mo
  • Webpack:未知属性“查询”?

    我正在练习使用 React 构建一个按钮 单击该按钮时计数器会加 1 我现在需要使用 Webpack 打包所有内容 以便可以在浏览器中运行它 我运行以下命令 webpack watch mode development 并得到以下错误 In
  • 全新安装时的 HDFS 空间使用情况

    我刚刚安装了 HDFS 并启动了该服务 并且已使用空间已经超过800MB 它代表什么 hdfs dfs df h Filesystem Size Used Available Use hdfs quickstart cloudera 802
  • Android 数据绑定:“使用‘.’的方法引用已弃用”

    在我的应用程序中使用数据绑定时 我在编译时收到以下警告 Warning Method references using is deprecated Instead of handler onItemClick use handler onI
  • 测试单元Spring boot:无法注册模拟bean

    我有两类测试 1 用于控制器类的单元测试 2 用于服务类的单元测试 如下所示 1 测试类控制器 RunWith SpringRunner class SpringBootTest AutoConfigureMockMvc public cl