java - How to overwrite a settermethod in Mockito? -


i can't seem figure out how mock simple setter method using mockito. have following class:

class myclass {     private someobject someobject;      public void setsomeobject(someobject someobject) {         this.someobject = someobject;     }      public someobject getsomeobject() {         return someobject;     } } 

now want when "setsomeobject" called new instance of "someobject" set. parameter within setter should ignored.

i need this:

myclass mockedclass = mock(myclass.class); when(mockedclass.setsomeobject([ignorewhatsinhere]))     .then(mockedclass.setsomeobject(new someobject(); 

however, can't seem syntax working this. can mocks work using getters(), because can return something. can't figure out how same setters().

all appreciated.

you should able use dothrow()|doanswer()|donothing()|doreturn() family of methods perform suitable action when testing methods return void including setters. instead of

when(mockedobject.somemethod()).thenreturn(something) 

you use doanswer() return custom answer object, although it's not terribly elegant, , might better off using stub:

doanswer(new answer() {      public object answer(invocationonmock invocation) {           //whatever code want run when method called           return null;   }}).when(mockedobject).somemethod(); } 

if trying return different values same getter call, may want @ mockito's consecutive stubbing calls, allow to, example, throw error first call of method, , return object second call.

see http://docs.mockito.googlecode.com/hg/org/mockito/mockito.html#12 more details on both of these.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -