How can I test a method that invokes protected (unwanted) methods from the parent class?

advertisements

I'm stuck in a very weird case. I have some specific code which I need to test. Here it is:

public class A {

    /*
     * The real method of real class is so big that I just don't want to test it.
     * That's why I use throwing an 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 also have a child class:

public class B extends A {

    @Override
    protected void mainMethod(Integer value) {
        if (value == 100500) {
            Integer result = super.generifiedMethod("abc", 100);
            super.method(result);
        }
        super.mainMethod(value);
    }
}

I need to cover the child class with tests.

I was trying a lot of combinations with PowerMockito, but none of them can verify invocation of protected methods of parent class. Also, I have a restriction on use only Mockito, PowerMockito and TestNG.

Here is my test code (one of variants):

@Test
public void should_invoke_parent_logic_methods_of_A_class() throws Exception {

    /* Given */
    A 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);

    /* Then */
    /**
     * Here I need to verify invocation of all methods of class A (generifiedMethod(), method(),
     * and mainMethod()). But I don't need them to be invoked because their logic is unwanted
     * to be tested in case of tests for class B.
     */
}

I would be appreciate for any suggestions how to test class B. Thanks.

Update

If I add into Then section this 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 the following error message:

Wanted but not invoked:
a.generifiedMethod("abc", 100);


Did you consider variant to change design of your classes and use composition instead of inheritance ? Then you will be able to just mock / spy instance of class A and inject it to instance of class B. In such case you will be able to configure whatever behavior that you need.

I really not sure doCallRealMethod() will make trick for you, cause you have option to mock method or invoke real one, but not both simultaneously.