{"record":{"id":"39956e415195b49e","repo":"mockito/mockito","slug":"the-type-type-simplename-is-an-interface","errorCode":null,"errorMessage":"the type 'TYPE_SIMPLENAME' is an interface.","messagePattern":"the type 'TYPE_SIMPLENAME' is an interface\\.","errorType":"exception","errorClass":"MockitoException","httpStatus":null,"severity":"error","filePath":"mockito-core/src/main/java/org/mockito/internal/util/reflection/FieldInitializer.java","lineNumber":120,"sourceCode":"\n    private void checkNotLocal(Field field) {\n        if (field.getType().isLocalClass()) {\n            throw new MockitoException(\n                    \"the type '\" + field.getType().getSimpleName() + \"' is a local class.\");\n        }\n    }\n\n    private void checkNotInner(Field field) {\n        Class<?> type = field.getType();\n        if (type.isMemberClass() && !isStatic(type.getModifiers())) {\n            throw new MockitoException(\n                    \"the type '\" + type.getSimpleName() + \"' is an inner non static class.\");\n        }\n    }\n\n    private void checkNotInterface(Field field) {\n        if (field.getType().isInterface()) {\n            throw new MockitoException(\n                    \"the type '\" + field.getType().getSimpleName() + \"' is an interface.\");\n        }\n    }\n\n    private void checkNotAbstract(Field field) {\n        if (Modifier.isAbstract(field.getType().getModifiers())) {\n            throw new MockitoException(\n                    \"the type '\" + field.getType().getSimpleName() + \"' is an abstract class.\");\n        }\n    }\n\n    private void checkNotEnum(Field field) {\n        if (field.getType().isEnum()) {\n            throw new MockitoException(\n                    \"the type '\" + field.getType().getSimpleName() + \"' is an enum.\");\n        }\n    }\n","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/mockito/mockito/blob/5a676bcd9ef9a10db46e1dc34e190eb46faa2687/mockito-core/src/main/java/org/mockito/internal/util/reflection/FieldInitializer.java#L102-L138","documentation":"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.","triggerScenarios":"Declaring a @Spy (or FieldInitializer-driven) field whose declared type is an interface, e.g. @Spy Runnable r; with no instance assigned.","commonSituations":"Copy-pasting @Mock into @Spy on an interface field; expecting Mockito to auto-generate an implementation for spy fields like it does for mocks.","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"],"exampleFix":"// before\n@Spy private List<String> list; // interface\n// after\n@Mock private List<String> list;\n// or\nprivate List<String> list = Mockito.spy(new ArrayList<>());","handlingStrategy":"type-guard","validationCode":"Class<?> t = field.getType();\nif (t.isInterface()) { /* use @Mock or spy a concrete impl */ }","typeGuard":"static boolean isConcreteClass(Class<?> c) {\n    return !c.isInterface() && !java.lang.reflect.Modifier.isAbstract(c.getModifiers());\n}","tryCatchPattern":null,"preventionTips":["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"],"tags":["mockito","reflection","interface"],"backgroundTag":"mockito-field-initialization-failed","analyzedSha":"5a676bcd9ef9a10db46e1dc34e190eb46faa2687","analyzedAt":"2026-09-05T19:32:58.607Z","contentChangedAt":"2026-09-05T19:32:58.607Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}