{"record":{"id":"64dfdfcfe1f005f0","repo":"apache/kafka","slug":"maxpollrecords-must-be-strictly-superior-to-0","errorCode":null,"errorMessage":"MaxPollRecords must be strictly superior to 0","messagePattern":"MaxPollRecords must be strictly superior to 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java","lineNumber":395,"sourceCode":"     */\n    public synchronized void addRecord(ConsumerRecord<K, V> record) {\n        ensureNotClosed();\n        TopicPartition tp = new TopicPartition(record.topic(), record.partition());\n        Set<TopicPartition> currentAssigned = this.subscriptions.assignedPartitions();\n        if (!currentAssigned.contains(tp))\n            throw new IllegalStateException(\"Cannot add records for a partition that is not assigned to the consumer\");\n        List<ConsumerRecord<K, V>> recs = records.computeIfAbsent(tp, k -> new ArrayList<>());\n        recs.add(record);\n    }\n\n    /**\n     * Sets the maximum number of records returned in a single call to {@link #poll(Duration)}.\n     *\n     * @param maxPollRecords the max.poll.records.\n     */\n    public synchronized void setMaxPollRecords(long maxPollRecords) {\n        if (maxPollRecords < 1) {\n            throw new IllegalArgumentException(\"MaxPollRecords must be strictly superior to 0\");\n        }\n        this.maxPollRecords = maxPollRecords;\n    }\n\n    /**\n     * Sets an exception to throw when {@link #poll(Duration)} is called.\n     *\n     * @param exception the exception to throw\n     */\n    public synchronized void setPollException(KafkaException exception) {\n        this.pollException = exception;\n    }\n\n    /**\n     * Sets an exception to throw when offset-related methods are called.\n     *\n     * @param exception the exception to throw\n     */","sourceCodeStart":377,"sourceCodeEnd":413,"githubUrl":"https://github.com/apache/kafka/blob/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/consumer/MockConsumer.java#L377-L413","documentation":"Thrown by MockConsumer.setMaxPollRecords when the supplied value is less than 1. maxpoll records controls how many records a single poll returns; zero or negative is meaningless and would break poll loops. The mock validates this so tests catch configuration errors immediately rather than producing odd poll behavior.","triggerScenarios":"Calling setMaxPollRecords(0) or a negative value; computing the value from config that defaulted to 0; test parameterization with a 0 entry.","commonSituations":"Tests parameterizing max.poll.records across a range that includes 0; config loader returning 0 for an unset key; refactor that changed the type from Optional<Long> to a primitive long defaulting to 0.","solutions":["Pass a value >= 1 (typical values match real max.poll.records, e.g. 500).","Validate the source config with a minimum bound before forwarding to setMaxPollRecords.","Default to a sane positive constant (e.g. 500) when config is missing."],"exampleFix":"// before\nmockConsumer.setMaxPollRecords(0);\n\n// after\nmockConsumer.setMaxPollRecords(500);","handlingStrategy":"validation","validationCode":"if (maxPollRecords < 1) throw new IllegalArgumentException(\"maxPollRecords must be >= 1\");\nmockConsumer.setMaxPollRecords(maxPollRecords);","typeGuard":"static boolean isValidMaxPoll(long v) {\n    return v >= 1;\n}","tryCatchPattern":"try {\n    mockConsumer.setMaxPollRecords(requested);\n} catch (IllegalArgumentException e) {\n    if (\"MaxPollRecords must be strictly superior to 0\".equals(e.getMessage())) {\n        mockConsumer.setMaxPollRecords(500); // safe default\n    } else throw e;\n}","preventionTips":["Bound-test parameter values in tests; exclude 0 and negatives from parameterized sources.","Default max.poll.records to a positive constant when config is missing.","Validate externalized config with a minimum of 1."],"tags":["consumer","mock-consumer","test","configuration","validation"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}