samedi 26 mars 2016

Powermock createPartialMockForAllMethodsExcept throwing null pointer because field is null?

I'm using powermock like so:

  DetailServiceImpl impl=PowerMock.createPartialMockForAllMethodsExcept(
DetailServiceImpl.class, "getServiceDetail", JSONObject.class,String.class);

However, when this line is run, "getServiceDetail" is executed and a null pointer exception is thrown because the method being mocked on the class DetailServiceImpl uses a class field that is autowired by spring, but on this mock it is null.

By the time I've made this assignment to the variable impl, the method is already run so I can't just do:

impl.setField(myFieldObjMock);

because it's already too late.

How can I fix this issue so that I can either set this field before the method is ran, or have powermock not leave this field null?

Aucun commentaire:

Enregistrer un commentaire