mockito/mockito · error · MockitoException
the type 'TYPE_SIMPLENAME' is an interface.
Error message
the type 'TYPE_SIMPLENAME' is an interface.
What it means
FieldInitializer cannot instantiate an interface, so it validates the field's declared type and throws this MockitoException if it is an interface. Mockito needs a concrete class to reflectively construct the instance.
Source
Thrown at mockito-core/src/main/java/org/mockito/internal/util/reflection/FieldInitializer.java:120
private void checkNotLocal(Field field) {
if (field.getType().isLocalClass()) {
throw new MockitoException(
"the type '" + field.getType().getSimpleName() + "' is a local class.");
}
}
private void checkNotInner(Field field) {
Class<?> type = field.getType();
if (type.isMemberClass() && !isStatic(type.getModifiers())) {
throw new MockitoException(
"the type '" + type.getSimpleName() + "' is an inner non static class.");
}
}
private void checkNotInterface(Field field) {
if (field.getType().isInterface()) {
throw new MockitoException(
"the type '" + field.getType().getSimpleName() + "' is an interface.");
}
}
private void checkNotAbstract(Field field) {
if (Modifier.isAbstract(field.getType().getModifiers())) {
throw new MockitoException(
"the type '" + field.getType().getSimpleName() + "' is an abstract class.");
}
}
private void checkNotEnum(Field field) {
if (field.getType().isEnum()) {
throw new MockitoException(
"the type '" + field.getType().getSimpleName() + "' is an enum.");
}
}
View on GitHub (pinned to 5a676bcd9e)
Solutions
- Use @Mock instead of @Spy for interface fields
- Assign a concrete instance: private Runnable r = Mockito.spy(new MyRunnable());
- Create the spy from an implementation class explicitly
Example fix
// before @Spy private List<String> list; // interface // after @Mock private List<String> list; // or private List<String> list = Mockito.spy(new ArrayList<>());
Defensive patterns
Strategy: type-guard
Validate before calling
Class<?> t = field.getType();
if (t.isInterface()) { /* use @Mock or spy a concrete impl */ } Type guard
static boolean isConcreteClass(Class<?> c) {
return !c.isInterface() && !java.lang.reflect.Modifier.isAbstract(c.getModifiers());
} Prevention
- Use @Mock for interfaces, @Spy only for concrete instances
- Assign interface-typed spy fields with Mockito.spy(new Impl())
- Check field types after refactors to interfaces
When it happens
Trigger: Declaring a @Spy (or FieldInitializer-driven) field whose declared type is an interface, e.g. @Spy Runnable r; with no instance assigned.
Common situations: Copy-pasting @Mock into @Spy on an interface field; expecting Mockito to auto-generate an implementation for spy fields like it does for mocks.
Related errors
- Access to <classNameField> was denied
- {cls.getName()} does not define a constructor
- Could not instantiate {cls.getName()}
- Could not resolve constructor of type {type.getName()} wit
- Could not resolve private lookup for {type.getTypeName()}
AI-assisted analysis of mockito/mockito@5a676bcd9e (2026-09-05).
Data as JSON: /api/errors/39956e415195b49e.
Report an issue: GitHub.