How to Use Mockito for Testing Abstract Classes?

Question

How can I test abstract classes using the Mockito framework without manually creating mocks?

@RunWith(MockitoJUnitRunner.class)
public abstract class AbstractService {
    public abstract int calculate(int a, int b);
}

public class ConcreteService extends AbstractService {
    @Override
    public int calculate(int a, int b) {
        return a + b;
    }
}

@Test
public void testAbstractClassMethod() {
    ConcreteService service = Mockito.mock(ConcreteService.class);
    Mockito.when(service.calculate(3, 2)).thenReturn(5);
    assertEquals(5, service.calculate(3, 2));
}

Answer

Testing abstract classes in Java can be challenging, especially when you want to avoid manually creating concrete implementations. Mockito provides a convenient way to create mock instances of abstract classes, allowing you to focus on testing the behavior of your code without the overhead of additional implementation details.

Mockito.mock(AbstractService.class);
Mockito.when(mockService.calculate(1, 2)).thenReturn(3);
int result = mockService.calculate(1, 2);
assertEquals(3, result);

Causes

  • Abstract classes cannot be instantiated directly, which complicates testing individual methods.
  • Manually creating concrete subclasses can lead to duplicated code and maintenance issues.

Solutions

  • Utilize Mockito’s ability to create mock instances of abstract classes.
  • Override specific methods in the mock to define expected behavior for the test.

Common Mistakes

Mistake: Forgetting to add @RunWith(MockitoJUnitRunner.class) to the test class.

Solution: Ensure your test class is running with the Mockito test runner to enable annotations.

Mistake: Attempting to mock final classes or methods, which Mockito cannot mock.

Solution: Use a concrete subclass or alternative testing strategy for final classes.

Helpers

  • Mockito
  • test abstract classes
  • Mockito testing guide
  • Java testing frameworks
  • mockito mock abstract class

Related Questions

⦿How to Fix java.lang.IllegalStateException: Only Fullscreen Opaque Activities Can Request Orientation in Android?

Learn how to resolve the IllegalStateException in Android when retrieving contacts in Oreo. Stepbystep guide with code snippets included.

⦿How to Retrieve the Class Name without the Package in Java?

Learn how to obtain the class name without the package in Java using the Class class. Stepbystep guide and code examples included.

⦿How to Delete All Files in a Directory Without Deleting the Directory Itself

Learn a simple oneliner solution to remove all files from a specific directory while keeping the directory intact using Java.

⦿Differences Between Class.isInstance and Class.isAssignableFrom in Java

Explore the differences between Class.isInstance and Class.isAssignableFrom in Java including their usage examples and common mistakes.

⦿How Many Threads Can a Java Virtual Machine Support?

Discover how many threads a Java Virtual Machine JVM can support including factors affecting thread limits by vendor and operating system.

⦿How to Convert an Existing Eclipse Java Project to a Maven Project?

Learn how to easily convert your Eclipse Java project to a Maven project using the Eclipse Maven plugin. Simplify your builds today

⦿How to Programmatically Show or Hide the Soft Keyboard on Android?

Learn how to control the visibility of the soft keyboard in your Android app using Kotlin or Java with expert tips and code examples.

⦿How to Efficiently Transform a List in Java: Map vs forEach with Stream API

Explore the efficiency of using map vs forEach methods in Java Stream API for transforming lists with detailed examples and insights.

⦿What is the Keyboard Shortcut for Copying and Pasting an Entire Line in Eclipse?

Learn the keyboard shortcut to easily copy and paste an entire line in Eclipse without having to highlight it plus troubleshooting tips.

⦿How to Properly Encode and Decode a String in Base64 Using Java?

Learn to encode and decode strings in Base64 using Java. Get examples common mistakes and debugging tips for effective implementation.

© Copyright 2025 - CodingTechRoom.com

close