如何使用mockito或Junit测试此方法?

时间:2018-05-18 06:51:55

标签: java unit-testing methods mockito

@覆盖         public void storeInfoInStaging(StagingInfo stagingInfo)抛出BusinessException {

        INotificaDao iNotificaDao = NotificaDaoFactory.getInstance().getNotificaDao();
        try {
            iNotificaDao.storeInfoInStaging(stagingInfo);
        } catch (DataException e) {
            LOGGER.error(e.getMessage(), e);
            throw new BusinessException(NotificaConstants.DB_ERROR_CODE, NotificaConstants.DB_ERROR_MESSAGE);
        }

    }

1 个答案:

答案 0 :(得分:1)

我不知道你的className,你需要将className替换为你自己的className。也许,你可以这样做,只需检查方法真正调用方法。

  @Mock
  private INotificationDao iNotificationDao;
  private ClassName className;

  @Before
  public void init() {
    className = spy(new ClassName());
  }

  @Test
  public void storeInfoInStagingGivenStaginInfoValid() {
    Mockito.doNothing().when(className).storeInfoStaging(stagingInfo);
    className.storeInfoStaging(storeInfo);
    Mockito.verify(iNotificaDao, atLeastOnce()).storeInfoStaging(staginInfo);
  }