java - How can I test this method using Mockito? -
i have method complex interaction between different objects test using mockito framework. appreciate guidelines. know code not mean much.
both getinstance()
methods static. mock obj3 , make if statement return true in test.
public myrequest getrequest(objectone obj1, objecttwo obj2) { objectthree obj3 = factoryone.getinstance().getlist().getobject(obj2.getid()); if(factorytwo.getinstance().isflagset("flag")){ .... } return new myrequest(....); }
actually, method invoked value obj3
variable not straight testable.
the root cause getinstance()
static method.
in fact, factoryone.getinstance()
should dependency of class under test , returned object should interface
in way, can define constructor sets dependency:
public myclass(foointerface foo){ this.foo = foo; }
and replace :
objectthree obj3 = factoryone.getinstance().getlist().getobject(obj2.getid());
by :
objectthree obj3 = foo.getlist().getobject(obj2.getid());
in application code, invoke constructor :
myclass c = new myclass(factoryone.getinstance().getlist());
and in test code, create mock foo
interface, set class under test via constructor :
foo foomock = mockito.mock(foo.class); myclass myclassundertest = new myclass(foomock);
then record mock behavior getlist()
return mocked object desired value invocation getobject()
in test.
Comments
Post a Comment