java - Use mock object in test method implementation -
i have question. there's method implementation using objects. i'd test method not using objects invoking.
class object1 { public void method1() { object2 object2 = new object2(); string info = object2.getinfo(); // ... // working info, want test // ... } } class object1test { @test public void testmethod1() { object1 object1 = new object1(); object1.method(); // want run method without invoking object2 } }
could me understand how in junit while testing method1() can mock using of object2 object2mock?
you need make changes class achieve trying do.
class object1 { private object2 object2 = new object2(); /* setter method object2 */ public void setobject2(object2 object2) { this.object2 = object2; } public void method1() { string info = object2.getinfo(); // ... // working info, want test // ... } }
now have call setter-method mocked object object2.
class object1test { @test public void testmethod1() { object1 object1 = new object1(); object1.setobject2(/* pass mocked object here */); object1.method(); // want run method without invoking object2 } }
you can use mockito
or jmock
create object mocks. can tell mockito return dummy response whenever getinfo()
invoked on object2
. example,
mockito.when(mockedobject2.getinfo()).thenreturn("dummy response");
Comments
Post a Comment