java - How can I test a method which invoke protected (unwanted) methods of parent class? -
i'm stuck in weird case. have specific code need test. here is:
public class {      /*      * real method of real class big don't want test it.      * that's why use throwing exception.      */     protected void method(integer result) {         throw new runtimeexception("oops!");     }      protected <t> t generifiedmethod(string s, t type) {         throw new runtimeexception("oops!");     }      protected void mainmethod(integer value) {         throw new runtimeexception("oops!");     } } i have child class:
public class b extends {      @override     protected void mainmethod(integer value) {         if (value == 100500) {             integer result = super.generifiedmethod("abc", 100);             super.method(result);         }         super.mainmethod(value);     } } i need cover child class tests.
i trying lot of combinations powermockito, none of them can verify invocation of protected methods of parent class. also, have restriction on use mockito, powermockito , testng.
here test code (one of variants):
@test public void should_invoke_parent_logic_methods_of_a_class() throws exception {      /* given */     aspy = powermockito.spy(new a());      powermockito.doreturn(250).when(aspy, "generifiedmethod", "abc", 100);     powermockito.donothing().when(aspy, "method", 250);     powermockito.suppress(method(a.class, "mainmethod", integer.class));      /* when */     aspy.mainmethod(100500);      /* */     /**      * here need verify invocation of methods of class (generifiedmethod(), method(),      * , mainmethod()). don't need them invoked because logic unwanted      * tested in case of tests class b.      */ } i appreciate suggestions how test class b. thanks.
update
if add then section code
mockito.verify(aspy, times(3)).mainmethod(100500); mockito.verify(aspy, times(1)).generifiedmethod("abc", 100); mockito.verify(aspy, times(1)).method(250); it gives me following error message:
wanted not invoked: a.generifiedmethod("abc", 100); 
did consider variant change design of classes , use composition instead of inheritance ? able mock / spy instance of class , inject instance of class b. in such case able configure whatever behavior need.
i not sure docallrealmethod() make trick you, cause have option mock method or invoke real one, not both simultaneously.
Comments
Post a Comment