使用Spring、JUnit和Mockito在自动装配的组件内部模拟方法

huangapple go评论124阅读模式
英文:

Mock a method inside autowired component using Spring, JUnit and Mockito

问题

我正在寻找一种方法来“模拟”一个Autowired组件内部的方法。

例如,我的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;

//正常成功场景
@Test
public void test03() {
    try {
        HouseholdUpdateServiceCall householdUpdateServiceCall = new HouseholdUpdateServiceCall();
        householdUpdateServiceCall.setCPCount(0);
        householdUpdateServiceCall.setHhId(1L);
        householdUpdateServiceCall.setPanelId(1L);
        
        
        //如何模拟doPersist内部使用的方法,该方法在persistService中自动装配?
        persistService.doPersist(householdUpdateServiceCall);
        ...

我正在寻找的是如何模拟persistService内部的一个方法(String result = meterManagementService.getResult())。例如,如何为meterManagementService.getResult()设置一个值“OK”?

感谢您的任何建议和帮助。

英文:

I'm looking for a way to "mock" a method inside an Autowired component.

For example, my PersistService contains a method like:

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

The Junit test contains:

    @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);
...

What I'm looking for is how to mock a method (String result = meterManagementService.getResult()) inside the persistService? For example how to set a value "OK" for meterManagementService.getResult()?

Thank you for any advice and help

答案1

得分: 2

你可以在测试中使用 @MockBean,就像这样(如果使用了 @RunWith(SpringRunner.class)):

@MockBean private MeterManagementService meterManagementService

它会被模拟并注入。

如果你没有使用或者无法使用 SpringRunner,可以使用构造函数/设置方法来设置你的依赖项 - 在那里你可以提供模拟实例并随意使用它。

英文:

You can just use @MockBean in your test like this (if @RunWith(SpringRunner.class))

@MockBean private MeterManagementService meterManagementService

It will be mocked and injected.

If you are not using or you cannot use SpringRunner use constructor/setter to set your dependencies - there you will be able to provide mocked instances and use it at will.

huangapple
  • 本文由 发表于 2020年3月16日 04:18:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/60697253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定