Pergunta

I have this piece of code:

new Expectations(){
    {

    mFubar.getModel();
    result = new Model();
    times = 1;

    mFubar.getModel().getAllDogs();
    result = new HashSet<Dogs>();
    times = 1;
    }
};

Unfortunately I always get a null value for mFubar.getModel().
How can I create a mock value for getModel() so mFubar.getModel().getAllDogs(); works correctly?

Foi útil?

Solução

You get a NPE because the second call to mFubar.getModel(), just like the first, returns null. You can't use recorded results inside an expectation recording block; these values are meant to be obtained from the code under test only.

Besides, it doesn't look like the Model class is being mocked here, so trying to record a call to getAllDogs() wouldn't work either. For that, you would need to declare a @Mocked Model model mock field or mock parameter.

Finally, the default return value for a mocked method having a collection (List, Set, Map, etc.) as its return type already is an empty collection. So, writing result = new HashSet<Dog>(); would be redundant anyway.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top