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() … Read more

Abstract class in Java

An abstract class is a class which cannot be instantiated. An abstract class is used by creating an inheriting subclass that can be instantiated. An abstract class does a few things for the inheriting subclass: Define methods which can be used by the inheriting subclass. Define abstract methods which the inheriting subclass must implement. Provide … Read more

When to use an interface instead of an abstract class and vice versa?

I wrote an article about that: Abstract classes and interfaces Summarizing: When we talk about abstract classes we are defining characteristics of an object type; specifying what an object is. When we talk about an interface and define capabilities that we promise to provide, we are talking about establishing a contract about what the object … Read more