junit - doNothing void method with params in other class -
i have 2 classes:
public class aclass{ public void meth1(){ bclass b = new bclass(); b.meth2();// dont want call method //buss logic } } public class bclass{ public void meth2(){ // logic } } currently creating junit test case meth1 in aclass. however, don't want call meth2 in bclass, execute busslogic in aclass.
the classes aclass , bclass fixed - cannot(and won't) change code on aclass , bclass.
i tried many things @injectmock , donothing using mokito , power mock, meth2 gets called when calling meth1 in aclass.
what can fix this?
you can powermockito. link explains details. below how might example.
@runwith(powermockrunner.class) @preparefortest(aclass.class) public class aclasstesting { @mock bclass mockb; @test public void testmeth1(){ //prepare mocks whennew(bclass.class).withnoarguments().thenreturn(mockb); donothing().when(mockb).meth2(); //run aclass instance = new aclass(); aclass.meth1(); //asserts , verify verifynew(bclass.class).withnoarguments(); verify(mockb, times(1)).meth2(); } } edit:
when mock out bclass instance using @mock replaces methods in instance mock methods. if want mock of methods in bclas, must spy bclass instance instead of mocking it. spy mocks methods want. replace @mock @spy in example meth2 blocked not other methods in bclass.
Comments
Post a Comment