{"record":{"id":"f86d8296e3b47822","repo":"apache/kafka","slug":"clientinstanceid-not-set-f86d82","errorCode":null,"errorMessage":"clientInstanceId not set","messagePattern":"clientInstanceId not set","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/consumer/MockShareConsumer.java","lineNumber":140,"sourceCode":"        return new HashMap<>();\n    }\n\n    @Override\n    public synchronized void commitAsync() {\n    }\n\n    @Override\n    public void setAcknowledgementCommitCallback(AcknowledgementCommitCallback callback) {\n    }\n\n    public synchronized void setClientInstanceId(final Uuid clientInstanceId) {\n        this.clientInstanceId = clientInstanceId;\n    }\n\n    @Override\n    public synchronized Uuid clientInstanceId(Duration timeout) {\n        if (clientInstanceId == null) {\n            throw new UnsupportedOperationException(\"clientInstanceId not set\");\n        }\n\n        return clientInstanceId;\n    }\n\n    @Override\n    public synchronized Map<MetricName, ? extends Metric> metrics() {\n        ensureNotClosed();\n        return Map.of();\n    }\n\n    @Override\n    public Optional<Integer> acquisitionLockTimeoutMs() {\n        return Optional.empty();\n    }\n\n    @Override\n    public void registerMetricForSubscription(KafkaMetric metric) {","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/apache/kafka/blob/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/consumer/MockShareConsumer.java#L122-L158","documentation":"MockShareConsumer is the share-group test double. Unlike a real consumer it never auto-generates a clientInstanceId; the value is null until the test injects one via setClientInstanceId(Uuid). clientInstanceId(Duration) is annotated @Override but its body simply throws UnsupportedOperationException when the field is still null, rather than blocking for the timeout.","triggerScenarios":"Calling mockShareConsumer.clientInstanceId(Duration) before any call to mockShareConsumer.setClientInstanceId(Uuid).","commonSituations":"Test code copied from a KafkaConsumer test that calls clientInstanceId for metrics/group assertions; running a test that expects a stable group member id without seeding it; or invoking metrics()-dependent paths that surface clientInstanceId.","solutions":["Call mockShareConsumer.setClientInstanceId(Uuid.randomUuid()) (or a deterministic Uuid) before any code path that reads clientInstanceId.","If you do not need clientInstanceId in the test, avoid calling it and any helper that reads it.","If the assertion must run before the id is known, restructure the test so setClientInstanceId runs in setup before the action under test."],"exampleFix":"// before\nMockShareConsumer<K,V> c = new MockShareConsumer<>();\nUuid id = c.clientInstanceId(Duration.ofSeconds(5)); // -> UnsupportedOperationException\n\n// after\nMockShareConsumer<K,V> c = new MockShareConsumer<>();\nc.setClientInstanceId(Uuid.METRIC_GROUP_ID_OFFSET_FETCHER); // or Uuid.randomUuid()\nUuid id = c.clientInstanceId(Duration.ofSeconds(5));","handlingStrategy":"validation","validationCode":"// Seed the id before any code path that reads it.\nif (mockShareConsumer.clientInstanceIdField == null /* or expose a hasClientInstanceId() */) {\n    mockShareConsumer.setClientInstanceId(Uuid.randomUuid());\n}","typeGuard":"null","tryCatchPattern":"// Treat UnsupportedOperationException as 'not yet set' and seed + retry once.\ntry {\n    return mock.clientInstanceId(Duration.ofSeconds(5));\n} catch (UnsupportedOperationException e) {\n    mock.setClientInstanceId(Uuid.randomUuid());\n    return mock.clientInstanceId(Duration.ofSeconds(5));\n}","preventionTips":["In @BeforeEach, always call setClientInstanceId(Uuid.randomUuid()) on MockShareConsumer.","Do not assert on clientInstanceId unless your test has explicitly seeded it."],"tags":["testing","mock","share-consumer","client-instance-id","kafka-clients"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}