{"record":{"id":"842cf619b5bb3d50","repo":"apache/kafka","slug":"the-partition-does-not-have-a-beginning-offset","errorCode":null,"errorMessage":"The partition {} does not have a beginning offset.","messagePattern":"The partition (.+?) does not have a beginning offset\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java","lineNumber":645,"sourceCode":"    }\n\n    @Override\n    public synchronized Map<TopicPartition, OffsetAndTimestamp> offsetsForTimes(Map<TopicPartition, Long> timestampsToSearch) {\n        throw new UnsupportedOperationException(\"Not implemented yet.\");\n    }\n\n    @Override\n    public synchronized Map<TopicPartition, Long> beginningOffsets(Collection<TopicPartition> partitions) {\n        if (offsetsException != null) {\n            RuntimeException exception = this.offsetsException;\n            this.offsetsException = null;\n            throw exception;\n        }\n        Map<TopicPartition, Long> result = new HashMap<>();\n        for (TopicPartition tp : partitions) {\n            Long beginningOffset = beginningOffsets.get(tp);\n            if (beginningOffset == null)\n                throw new IllegalStateException(\"The partition \" + tp + \" does not have a beginning offset.\");\n            result.put(tp, beginningOffset);\n        }\n        return result;\n    }\n\n    @Override\n    public synchronized Map<TopicPartition, Long> endOffsets(Collection<TopicPartition> partitions) {\n        if (offsetsException != null) {\n            RuntimeException exception = this.offsetsException;\n            this.offsetsException = null;\n            throw exception;\n        }\n        Map<TopicPartition, Long> result = new HashMap<>();\n        for (TopicPartition tp : partitions) {\n            Long endOffset = endOffsets.get(tp);\n            if (endOffset == null)\n                throw new IllegalStateException(\"The partition \" + tp + \" does not have an end offset.\");\n            result.put(tp, endOffset);","sourceCodeStart":627,"sourceCodeEnd":663,"githubUrl":"https://github.com/apache/kafka/blob/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java#L627-L663","documentation":"Thrown by MockConsumer.beginningOffsets when a requested partition has no entry in the mock's beginningOffsets map. MockConsumer does not compute offsets from a broker; the test must seed them via updateBeginningOffsets (or the equivalent set method). Asking for an unseeded partition is a test setup bug.","triggerScenarios":"Calling beginningOffsets(tp) without first updateBeginningOffsets(Map.of(tp, 0L)); partition name/number mismatch between seed and query; calling beginningOffsets for partitions never assigned.","commonSituations":"Test scaffolding that seeds only some partitions; refactor that added partitions without updating the seed; tests using positional logic that derives partitions not present in the seed map.","solutions":["Call mock.updateBeginningOffsets(Map.of(tp, 0L)) for every partition the code under test will query.","Derive partitions in the seed map from the same constants used elsewhere in the test.","Use a helper that seeds both beginning and end offsets for the full assignment."],"exampleFix":"// before\nmock.assign(Set.of(tp));\nLong beg = mock.beginningOffsets(Set.of(tp)).get(tp); // throws\n\n// after\nmock.assign(Set.of(tp));\nmock.updateBeginningOffsets(Map.of(tp, 0L));\nLong beg = mock.beginningOffsets(Set.of(tp)).get(tp);","handlingStrategy":"validation","validationCode":"Set<TopicPartition> missing = partitions.stream()\n    .filter(tp -> !seededBeginningOffsets.containsKey(tp)).collect(Collectors.toSet());\nif (!missing.isEmpty()) mockConsumer.updateBeginningOffsets(\n    missing.stream().collect(Collectors.toMap(tp -> tp, tp -> 0L)));\nreturn mockConsumer.beginningOffsets(partitions);","typeGuard":"static boolean allBeginningSeeded(MockConsumer<?,?> m, Collection<TopicPartition> tps) {\n    // expose a known-seed set in the test; verify it covers tps\n    return knownBeginningSeed.containsAll(tps);\n}","tryCatchPattern":"try {\n    return mockConsumer.beginningOffsets(partitions);\n} catch (IllegalStateException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"does not have a beginning offset\")) {\n        mockConsumer.updateBeginningOffsets(partitions.stream().collect(Collectors.toMap(tp -> tp, tp -> 0L)));\n        return mockConsumer.beginningOffsets(partitions);\n    }\n    throw e;\n}","preventionTips":["Seed beginning offsets in a single setup helper for the full assignment.","Derive partitions in the seed map from the same constants used in assign().","Add a test helper that seeds both beginning and end offsets atomically."],"tags":["consumer","mock-consumer","test","offsets","illegal-state"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}