java - Mocking a local object inside a method of SUT using Mockito or PowerMocktio -


i've class method below creates local object , calls method on local object.

public class myclass {     public somereturn mymethod(){         myotherclass otherclassobject = new myotherclass();         boolean retbool = otherclassobject.otherclassmethod();         if(retbool){             //         }     } }  public class myclasstest {     @test     public testmymethod(){         myclass myclassobj = new myclass();         myclassobj.mymethod();         // please me here..      } } 

when i'm testing mymethod, want mock otherclassobject.otherclassmethod return of choice. otherclassmethod class message queues , don't want in unit test. want return true when otherclassobj.otherclassmethod(). know must have used factory myotherclass instantiation in case it's legacy code , don't want change code now. see mockito doesn't provide facility mock myotherclass in case possible powermockito. however, not find example above scenario found static class. how should mock local object inside method of sut ?

i referred other os questions - mocking methods of local scope objects mockito not helpful.

a code example of great help.

if using powermockito can use whennew method

it should this:

@runwith(powermockrunner.class) @preparefortest(myclass.class)  //tells powermock modify myclass intercept calls new somewhere inside public class myclasstest{      @test     public void test(){           myotherclass mymock = createmock(myotherclass.class);           //this intercept calls "new myotherclass()" in myclass           whennew( myotherclass.class).withnoarguments().thenreturn( mymock) );           ... rest of test goes here     } 

also other post has example code powermockito mocking whennew not taking affect


Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

c# - SharpSsh Command Execution -

python - Specify path of savefig with pylab or matplotlib -