{"record":{"id":"9eccc54ac248164e","repo":"mockito/mockito","slug":"this-combination-of-annotations-is-not-permitted-o","errorCode":null,"errorMessage":"This combination of annotations is not permitted on a single field:\n@${annotationOne} and @${annotationTwo}","messagePattern":"This combination of annotations is not permitted on a single field:\n@(.+?) and @(.+?)","errorType":"exception","errorClass":"MockitoException","httpStatus":null,"severity":"error","filePath":"mockito-core/src/main/java/org/mockito/internal/configuration/injection/scanner/InjectMocksScanner.java","lineNumber":65,"sourceCode":"    @SuppressWarnings(\"unchecked\")\n    private Set<Field> scan() {\n        Set<Field> mockDependentFields = new HashSet<>();\n        Field[] fields = clazz.getDeclaredFields();\n        for (Field field : fields) {\n            if (null != field.getAnnotation(InjectMocks.class)) {\n                assertNoAnnotations(field, Mock.class, Captor.class);\n                mockDependentFields.add(field);\n            }\n        }\n\n        return mockDependentFields;\n    }\n\n    private static void assertNoAnnotations(\n            Field field, Class<? extends Annotation>... annotations) {\n        for (Class<? extends Annotation> annotation : annotations) {\n            if (field.isAnnotationPresent(annotation)) {\n                throw unsupportedCombinationOfAnnotations(\n                        annotation.getSimpleName(), InjectMocks.class.getSimpleName());\n            }\n        }\n    }\n}\n","sourceCodeStart":47,"sourceCodeEnd":71,"githubUrl":"https://github.com/mockito/mockito/blob/5a676bcd9ef9a10db46e1dc34e190eb46faa2687/mockito-core/src/main/java/org/mockito/internal/configuration/injection/scanner/InjectMocksScanner.java#L47-L71","documentation":"InjectMocksScanner scans test class fields for @InjectMocks and asserts that an @InjectMocks field carries none of a set of incompatible annotations. If it finds one, it throws unsupportedCombinationOfAnnotations(annotationName, \"InjectMocks\"): 'This combination of annotations is not permitted on a single field: @X and @InjectMocks'. @InjectMocks marks the target of injection and cannot double as a mock/spy/captor.","triggerScenarios":"Declaring a field with @InjectMocks together with @Mock, @Spy, @Captor (whatever annotations are passed to assertNoAnnotations) and running openMocks/annotation processing via scan().","commonSituations":"Misunderstanding @InjectMocks as a flavor of @Mock and stacking both; auto-adding annotations via IDE templates; copy-paste between test classes.","solutions":["Keep only @InjectMocks on the field and let Mockito instantiate it","Annotate dependencies with @Mock and leave the system-under-test with just @InjectMocks","If you need a real instance, instantiate it manually and drop both annotations"],"exampleFix":"// before\n@Mock\n@InjectMocks private Service svc;\n// after\n@InjectMocks private Service svc;","handlingStrategy":"validation","validationCode":"if (field.isAnnotationPresent(InjectMocks.class) &&\n    (field.isAnnotationPresent(Mock.class) || field.isAnnotationPresent(Spy.class) || field.isAnnotationPresent(Captor.class)))\n    throw new IllegalStateException(\"@InjectMocks must be the only mockito annotation on \" + field.getName());","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat @InjectMocks as the target, not a mock — never combine it with @Mock/@Spy/@Captor","Check IDE annotation templates that auto-add @Mock","Review fields when migrating between mockito annotation styles"],"tags":["mockito","annotations","injectmocks"],"backgroundTag":"conflicting-field-annotations","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"}