How to mock protected subclass method inherited from abstract class?

我怕爱的太早我们不能终老 提交于 2019-12-05 23:13:24

问题


How to use Mockito or PowerMock to mock a protected method that is realized by a subclass, but inherited from an abstract super class?

In other words, I want to test "doSomething" method while mocking the "doSomethingElse".

Abstract super class

public abstract class TypeA {

    public void doSomething() {     

        // Calls for subclass behavior
        doSomethingElse();      
    }

    protected abstract String doSomethingElse();

}

Subclass implementation

public class TypeB extends TypeA {

    @Override
    protected String doSomethingElse() {
        return "this method needs to be mocked";
    }

}

Solution

Answers given here are correct and will work if classes involved are in the same package.

But if different packages are involved one option is to user PowerMock. The following example worked for me. Of course there might be other ways of doing it, this is one that works.

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ TypeB.class })
public class TestAbstract {

    @Test
    public void test_UsingPowerMock() throws Exception {
        // Spy a subclass using PowerMock
        TypeB b = PowerMockito.spy(new TypeB());
        String expected = "some value for mock";

        // Mock a method by its name using PowerMock again
        PowerMockito.doReturn(expected).when(b, "doSomethingElse");

        // Calls the 
        String actual = b.doSomething();
        assertEquals(expected, actual);     

    }
}

Note: Tests done using Java 5, jUnit 4.11, Mockito 1.9.0 and PowerMock 1.4.12.


回答1:


You can use Mockito.CALLS_REAL_METHODS when mocking the abstract method. This will call the originals methods of the class and you can mock all abstract methods by yourself.

TypeA typeA = mock(TypeA.class, Mockito.CALLS_REAL_METHODS);
when(typeA.doSomethingElse()).thenReturn("Hello");
typeA.doSomething();

Or you test directly on the TypeB with a spy:

TypeB typeB = spy(new TypeB());
when(typeB.doSomethingElse()).thenReturn("Hello");
typeB.doSomething();



回答2:


I suggest using Mockito for that:

// Create a new Mock
final TypeA a = Mockito.mock(TypeA.class, Mockito.CALLS_REAL_METHODS);

// Call the method
a.doSomething();

// Now verify that our mocked class' method was called
Mockito.verify(a, Mockito.times(1)).doSomethingElse();



回答3:


You can test your abtract class using mockito in the following way

TypeA typA = Mockito.mock(TypeA.class, Mockito.CALLS_REAL_METHODS);
when(typA.doSomethingElse()).thenReturn("doSomethingElse");
Assert.assertSomething(typeA.doSomething());



回答4:


To mock methods that returns void in abstract classes we could use:

MyAbstractClass abs = Mockito.mock(MyAbstractClass.class);
Mockito.doNothing().when(abs).myMethod(arg1,arg2....));

We can replace arguments with Mockito.anyString() etc as per requirements.



来源:https://stackoverflow.com/questions/32073116/how-to-mock-protected-subclass-method-inherited-from-abstract-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!