Using Mockito to test abstract classes Using Mockito to test abstract classes java java

Using Mockito to test abstract classes


The following suggestion let's you test abstract classes without creating a "real" subclass - the Mock is the subclass.

use Mockito.mock(My.class, Mockito.CALLS_REAL_METHODS), then mock any abstract methods that are invoked.

Example:

public abstract class My {  public Result methodUnderTest() { ... }  protected abstract void methodIDontCareAbout();}public class MyTest {    @Test    public void shouldFailOnNullIdentifiers() {        My my = Mockito.mock(My.class, Mockito.CALLS_REAL_METHODS);        Assert.assertSomething(my.methodUnderTest());    }}

Note: The beauty of this solution is that you do not have to implement the abstract methods, as long as they are never invoked.

In my honest opinion, this is neater than using a spy, since a spy requires an instance, which means you have to create an instantiatable subclass of your abstract class.


If you just need to test some of the concrete methods without touching any of the abstracts, you can use CALLS_REAL_METHODS (see Morten's answer), but if the concrete method under test calls some of the abstracts, or unimplemented interface methods, this won't work -- Mockito will complain "Cannot call real method on java interface."

(Yes, it's a lousy design, but some frameworks, e.g. Tapestry 4, kind of force it on you.)

The workaround is to reverse this approach -- use the ordinary mock behavior (i.e., everything's mocked/stubbed) and use doCallRealMethod() to explicitly call out the concrete method under test. E.g.

public abstract class MyClass {    @SomeDependencyInjectionOrSomething    public abstract MyDependency getDependency();    public void myMethod() {        MyDependency dep = getDependency();        dep.doSomething();    }}public class MyClassTest {    @Test    public void myMethodDoesSomethingWithDependency() {        MyDependency theDependency = mock(MyDependency.class);        MyClass myInstance = mock(MyClass.class);        // can't do this with CALLS_REAL_METHODS        when(myInstance.getDependency()).thenReturn(theDependency);        doCallRealMethod().when(myInstance).myMethod();        myInstance.myMethod();        verify(theDependency, times(1)).doSomething();    }}

Updated to add:

For non-void methods, you'll need to use thenCallRealMethod() instead, e.g.:

when(myInstance.myNonVoidMethod(someArgument)).thenCallRealMethod();

Otherwise Mockito will complain "Unfinished stubbing detected."


You can achieve this by using a spy (use the latest version of Mockito 1.8+ though).

public abstract class MyAbstract {  public String concrete() {    return abstractMethod();  }  public abstract String abstractMethod();}public class MyAbstractImpl extends MyAbstract {  public String abstractMethod() {    return null;  }}// your test code belowMyAbstractImpl abstractImpl = spy(new MyAbstractImpl());doReturn("Blah").when(abstractImpl).abstractMethod();assertTrue("Blah".equals(abstractImpl.concrete()));