unit testing - mock a public method of other class in a method (java) -
i new mockito. code:
public class a{ public a{ ... b.fff(); //the function want mock ... } } public class b{ public boolean fff(){ ... ... //connect db ... } }
for unit test,
public class atest{ @test public void test(){ mock_a = new a(); assert.assertnotnull(mock_a); } }
because of function "b.fff()" need connect db,so mock function "b.fff()" return true or false let test can work without environment.
i tried code like:
public class atest{ @test public void test(){ powermockito.when(b.fff()).thenreturn(true); assert.assertnotnull(new a()); } }
but isn't working.
is there solution using mockito(or powermock)?
thanks.
what want here inject mock of b a. long term require setting inversion of control container, if simple test app can start (pseudo code here ... i'm typing on phone):
public class a{ public a(b binstance){ this.b = binstance; } public void foo() { b.dosomethingwithdb(); } }
in test, mock b , set expected return value, inject constructor of a.
do reading on ioc - make testing easier , make better programmer in long run.
Comments
Post a Comment