Source:
<code>private static void verifyReceivedAllRecords(final Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> expectedRecords, final Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> receivedRecords) { if (expectedRecords.size() != receivedRecords.size()) { throw new RuntimeException("Result verification failed. Received " + receivedRecords.size() + " records but expected " + expectedRecords.size()); } final StringDeserializer stringDeserializer = new StringDeserializer(); final IntegerDeserializer integerDeserializer = new IntegerDeserializer(); for (final Map.Entry<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> partitionRecords : receivedRecords.entrySet()) { final TopicPartition inputTopicPartition = new TopicPartition("data", partitionRecords.getKey().partition()); final Iterator<ConsumerRecord<byte[], byte[]>> expectedRecord = expectedRecords.get(inputTopicPartition).iterator(); <START> for (final ConsumerRecord<byte[], byte[]> receivedRecord : partitionRecords.getValue().subList(0, partitionRecords.getValue().size() - 1)) { <END> final ConsumerRecord<byte[], byte[]> expected = expectedRecord.next(); final String receivedKey = stringDeserializer.deserialize(receivedRecord.topic(), receivedRecord.key()); final int receivedValue = integerDeserializer.deserialize(receivedRecord.topic(), receivedRecord.value()); final String expectedKey = stringDeserializer.deserialize(expected.topic(), expected.key()); final int expectedValue = integerDeserializer.deserialize(expected.topic(), expected.value()); if (!receivedKey.equals(expectedKey) || receivedValue != expectedValue) { throw new RuntimeException("Result verification failed for " + receivedRecord + " expected <" + expectedKey + "," + expectedValue + "> but was <" + receivedKey + "," + receivedValue + ">"); } } } }</code><technical_language>logic fragile partitionRecords is empty. For -1 cases, add more dummy record array checked, remove element derived array maintain verification</technical_language>

Target:
private static void verifyReceivedAllRecords(final Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> expectedRecords, final Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> receivedRecords) { if (expectedRecords.size() != receivedRecords.size()) { throw new RuntimeException("Result verification failed. Received " + receivedRecords.size() + " records but expected " + expectedRecords.size()); } final StringDeserializer stringDeserializer = new StringDeserializer(); final IntegerDeserializer integerDeserializer = new IntegerDeserializer(); for (final Map.Entry<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> partitionRecords : receivedRecords.entrySet()) { final TopicPartition inputTopicPartition = new TopicPartition("data", partitionRecords.getKey().partition()); final Iterator<ConsumerRecord<byte[], byte[]>> expectedRecord = expectedRecords.get(inputTopicPartition).iterator(); for (final ConsumerRecord<byte[], byte[]> receivedRecord : partitionRecords.getValue()) { final ConsumerRecord<byte[], byte[]> expected = expectedRecord.next(); final String receivedKey = stringDeserializer.deserialize(receivedRecord.topic(), receivedRecord.key()); final int receivedValue = integerDeserializer.deserialize(receivedRecord.topic(), receivedRecord.value()); final String expectedKey = stringDeserializer.deserialize(expected.topic(), expected.key()); final int expectedValue = integerDeserializer.deserialize(expected.topic(), expected.value()); if (!receivedKey.equals(expectedKey) || receivedValue != expectedValue) { throw new RuntimeException("Result verification failed for " + receivedRecord + " expected <" + expectedKey + "," + expectedValue + "> but was <" + receivedKey + "," + receivedValue + ">"); } } } }