开发者社区> 问答> 正文

使用Spring,JUnit和Mockito在自动装配的组件中模拟方法

我正在寻找一种“模拟”自动装配组件内部方法的方法。

例如,my PersistService包含如下方法:

@Autowired
    MeterManagementService meterManagementService;

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public void doPersist(HouseholdUpdateServiceCall householdUpdateServiceCall) throws Exception {
        LOG.info("doPersist start()");
        if (householdUpdateServiceCall.getHhId() > -1) {
            saveUboPanelInfo(householdUpdateServiceCall);
            saveUboSites(householdUpdateServiceCall);
            if (householdUpdateServiceCall.getHouseholdUpdateRequest() != null
                    && householdUpdateServiceCall.getHouseholdUpdateRequest().getPeopleMeter() != null
                    && householdUpdateServiceCall.getHouseholdUpdateRequest().getPeopleMeter().getPeople() != null
                    && householdUpdateServiceCall.getHouseholdUpdateRequest().getPeopleMeter().getPeople().getPerson() != null) {

                Integer panelId = new Long(householdUpdateServiceCall.getPanelId()).intValue();
                Integer hhId = new Long(householdUpdateServiceCall.getHhId()).intValue();
                Integer personCount = householdUpdateServiceCall.getHouseholdUpdateRequest().getPeopleMeter().getPeople().getPerson().size();

                LOG.info("storePersonCount for panelId: " + panelId + ", hhId: " + hhId + ", personCount: " + personCount);
                uboPanelDao.storePersonCount(hhId, personCount, panelId);
            }

        String result = meterManagementService.getResult();
        LOG.info("The result is: " + result);     
        LOG.info("doPersist end()");
        }
    }

Junit测试包含:

@Autowired
    PersistService persistService;

    //normal success scenario
    @Test
    public void test03() {
        try {
            HouseholdUpdateServiceCall householdUpdateServiceCall = new HouseholdUpdateServiceCall();
            householdUpdateServiceCall.setCPCount(0);
            householdUpdateServiceCall.setHhId(1L);
            householdUpdateServiceCall.setPanelId(1L);


            //how to mock the method which is used inside doPersist and it is autowired in persistService?
            persistService.doPersist(householdUpdateServiceCall);
            ...

我要寻找的是如何在中模拟方法(String result = meterManagementService.getResult())persistService?例如,如何为它设置值“ OK” meterManagementService.getResult()?

感谢您的任何建议和帮助

问题来源:Stack Overflow

展开
收起
montos 2020-03-23 19:10:31 474 0
1 条回答
写回答
取消 提交回答
  • 您可以@MockBean像这样在测试中使用(如果@RunWith(SpringRunner.class))

    @MockBean private MeterManagementService meterManagementService

    它将被嘲笑并注入。

    如果您不使用或不能使用SpringRunner构造函数/设置器来设置依赖项,那么您将能够提供模拟实例并随意使用它。

    回答来源:Stack Overflow

    2020-03-23 19:11:38
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多