I have two obj, obj1 and obj2, obj2 is attribute in obj1 as
public class MyObject1
{
MyObject2 myObject2;
public MyObject2 getMyObject2()
{
return myObject2;
}
public void setMyObject2(MyObject2 myObject2)
{
this.myObject2 = myObject2;
}
}
In my service, it is initialized obj1, and get obj2 as
public void myTest2(){
MyObject1 myObject1 = new MyObject1();
MyObject2 myObject2 = myObject1.getMyObject2();
}
In my test, I mock the obj2, I want when(obj1.getObj2).thenReturn(obj2), this obj2 is the mock object, as
public void test3(){
MyObject2 myObject2 = mock(MyObject2.class);
MyObject1 myObject1 = spy(new MyObject1());
doReturn(myObject2).when(myObject1).getMyObject2();
myService.myTest2();
}
But now myObject2 I got from test is null, not the mocked myObject2. How can I get mocked myObject2 in my test?
Since we cannot use mockito without Reflection to mock a local variable, the problem is dead end for now