android - How verify the order of mock methods in JUnit -
i have class these structure , need test behaviour of onrequestlistoflunchsfinished
interface
@override public void getlistoflunchs(final onrequestlistoflunchsfinished callback) { zip().onerrorresumenext(new function<throwable, observablesource<? extends lunchserviceresponse>>() { @override public observablesource<? extends lunchserviceresponse> apply(@nonnull throwable throwable) throws exception { callback.onerror(new runtimeexception(throwable)); callback.onend(); return observable.empty(); } }).subscribe(new consumer<lunchserviceresponse>() { @override public void accept(lunchserviceresponse response) throws exception { list<lunch> result = new arraylist<>(); list<ingredientresponsevo> ingredients = response.getingredients(); map<integer, ingredient> hash = new hashmap<integer, ingredient>(); (ingredientresponsevo vo : ingredients) hash.put(vo.id, new ingredient(vo.id, vo.name, new bigdecimal(vo.price.tostring()), vo.image)); for(infolunchresponsevo vo: response.getlunch()){ lunch lunch = new lunch(); lunch.setid(vo.id); lunch.setimage(vo.image); lunch.setname(vo.name); for(integer id : vo.ingredients){ ingredient ingredient = hash.get(id); lunch.addingredient(ingredient); } result.add(lunch); } callback.onsuccess(result); callback.onend(); } }); callback.onstart(); } private observable<lunchserviceresponse> zip(){ return observable.zip(getrequestoflistoflunchs(), getrequestoflistofingredients(), new bifunction<list<infolunchresponsevo>, list<ingredientresponsevo>, lunchserviceresponse>() { @override public lunchserviceresponse apply(@nonnull list<infolunchresponsevo> infolunchresponsevos, @nonnull list<ingredientresponsevo> ingredientresponsevos) throws exception { return new lunchserviceresponse(infolunchresponsevos, ingredientresponsevos); } }); }
i have test method
@test public void teste(){ list<ingredientresponsevo> ingredients = collections.emptylist(); list<infolunchresponsevo> lunchs = collections.emptylist(); when(mockapi.getlistofingredients()).thenreturn(observable.just(ingredients)); when(mockapi.getlunchs()).thenreturn(observable.just(lunchs)); mockimplementation.getlistoflunchs(callback); inorder order = inorder(callback); order.verify(callback).onstart(); order.verify(callback).onsuccess(anylist()); order.verify(callback).onend(); order.verifynomoreinteractions(); }
but receiving exception:
org.mockito.exceptions.verification.verificationinorderfailure: verification in order failure wanted not invoked: callback.onsuccess(<any>);
if this:
callback.onstart(); callback.onsuccess(collections.<lunch>emptylist()); callback.onend(); inorder order = inorder(callback); order.verify(callback).onstart(); order.verify(callback).onsuccess(anylist()); order.verify(callback).onend(); order.verifynomoreinteractions();
this works.
how verify calls of mock callback
?
you must not use inorder
object.
mockimplementation.getlistoflunchs(callback); mockito.verify(callback).onstart(); mockito.verify(callback).onsuccess(anylist()); mockito.verify(callback).onend(); mockito.verifynomoreinteractions();
Comments
Post a Comment