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

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

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

问题

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

例如,我的PersistService包含一个类似以下的方法:

  1. @Autowired
  2. MeterManagementService meterManagementService;
  3. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  4. @Override
  5. public void doPersist(HouseholdUpdateServiceCall householdUpdateServiceCall) throws Exception {
  6. LOG.info("doPersist start()");
  7. if (householdUpdateServiceCall.getHhId() > -1) {
  8. saveUboPanelInfo(householdUpdateServiceCall);
  9. saveUboSites(householdUpdateServiceCall);
  10. if (householdUpdateServiceCall.getHouseholdUpdateRequest() != null
  11. && householdUpdateServiceCall.getHouseholdUpdateRequest().getPeopleMeter() != null
  12. && householdUpdateServiceCall.getHouseholdUpdateRequest().getPeopleMeter().getPeople() != null
  13. && householdUpdateServiceCall.getHouseholdUpdateRequest().getPeopleMeter().getPeople().getPerson() != null) {
  14. Integer panelId = new Long(householdUpdateServiceCall.getPanelId()).intValue();
  15. Integer hhId = new Long(householdUpdateServiceCall.getHhId()).intValue();
  16. Integer personCount = householdUpdateServiceCall.getHouseholdUpdateRequest().getPeopleMeter().getPeople().getPerson().size();
  17. LOG.info("storePersonCount for panelId: " + panelId + ", hhId: " + hhId + ", personCount: " + personCount);
  18. uboPanelDao.storePersonCount(hhId, personCount, panelId);
  19. }
  20. String result = meterManagementService.getResult();
  21. LOG.info("The result is: " + result);
  22. LOG.info("doPersist end()");
  23. }
  24. }

Junit测试包含:

  1. @Autowired
  2. PersistService persistService;
  3. //正常成功场景
  4. @Test
  5. public void test03() {
  6. try {
  7. HouseholdUpdateServiceCall householdUpdateServiceCall = new HouseholdUpdateServiceCall();
  8. householdUpdateServiceCall.setCPCount(0);
  9. householdUpdateServiceCall.setHhId(1L);
  10. householdUpdateServiceCall.setPanelId(1L);
  11. //如何模拟doPersist内部使用的方法,该方法在persistService中自动装配?
  12. persistService.doPersist(householdUpdateServiceCall);
  13. ...

我正在寻找的是如何模拟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:

  1. @Autowired
  2. MeterManagementService meterManagementService;
  3. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  4. @Override
  5. public void doPersist(HouseholdUpdateServiceCall householdUpdateServiceCall) throws Exception {
  6. LOG.info("doPersist start()");
  7. if (householdUpdateServiceCall.getHhId() > -1) {
  8. saveUboPanelInfo(householdUpdateServiceCall);
  9. saveUboSites(householdUpdateServiceCall);
  10. if (householdUpdateServiceCall.getHouseholdUpdateRequest() != null
  11. && householdUpdateServiceCall.getHouseholdUpdateRequest().getPeopleMeter() != null
  12. && householdUpdateServiceCall.getHouseholdUpdateRequest().getPeopleMeter().getPeople() != null
  13. && householdUpdateServiceCall.getHouseholdUpdateRequest().getPeopleMeter().getPeople().getPerson() != null) {
  14. Integer panelId = new Long(householdUpdateServiceCall.getPanelId()).intValue();
  15. Integer hhId = new Long(householdUpdateServiceCall.getHhId()).intValue();
  16. Integer personCount = householdUpdateServiceCall.getHouseholdUpdateRequest().getPeopleMeter().getPeople().getPerson().size();
  17. LOG.info("storePersonCount for panelId: " + panelId + ", hhId: " + hhId + ", personCount: " + personCount);
  18. uboPanelDao.storePersonCount(hhId, personCount, panelId);
  19. }
  20. String result = meterManagementService.getResult();
  21. LOG.info("The result is: " + result);
  22. LOG.info("doPersist end()");
  23. }
  24. }

The Junit test contains:

  1. @Autowired
  2. PersistService persistService;
  3. //normal success scenario
  4. @Test
  5. public void test03() {
  6. try {
  7. HouseholdUpdateServiceCall householdUpdateServiceCall = new HouseholdUpdateServiceCall();
  8. householdUpdateServiceCall.setCPCount(0);
  9. householdUpdateServiceCall.setHhId(1L);
  10. householdUpdateServiceCall.setPanelId(1L);
  11. //how to mock the method which is used inside doPersist and it is autowired in persistService?
  12. persistService.doPersist(householdUpdateServiceCall);
  13. ...

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)):

  1. @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:

确定