Pregunta

I have this NonStrictExpectation in my JUnit test case:

  new NonStrictExpectations(mCurrencyDao) {
      {
        invoke(mCurrencyDao, "readSqlQuery", withAny(String.class));
        result = prepareTestSQL(pAllKeysForTest);
        times = 1;
      }
    };
  mCurrencyDao.loadAll(lToday);

which works pretty fine and leads to the expected result. Now I had changed my code to this:

  new NonStrictExpectations(mCurrencyDao) {
      {
        invoke(mCurrencyDao, "readSqlQuery", withAny(String.class));
        result = prepareTestSQL(pAllKeysForTest);
        times = 1;
      }
    };
  mCurrencyDao.loadAll(lToday);
  mCurrencyDao.loadAll(lTomorrow);

the second call mCurrencyDao.loadAll(lTomorrow); has to be executed without my NonStrictExpectations.

Is there an easy way to remove my previous defined NonStrictExpectations after I called mCurrencyDao.loadAll(lToday);?

¿Fue útil?

Solución

You can use the mockit.Invocation class to invoke the original method that has been mocked by Deencapsulation.invoke(). The following code snippet delegates to original method on second method invocation.

new NonStrictExpectations(mCurrencyDao) {
    {
        invoke(mCurrencyDao, "readSqlQuery", withAny(String.class));
        result = new Delegate<String>() {
            @SuppressWarnings("unused")
            String delegate(Invocation invocation) {
                if (invocation.getInvocationIndex() == 0) {
                    return prepareTestSQL(pAllKeysForTest);
                } else {
                    return invocation.proceed();
                }
            }
        };
        times = 2;
    }
};
mCurrencyDao.loadAll(lToday);
mCurrencyDao.loadAll(lTomorrow);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top