Back to all reviewers

comprehensive test assertions

apache/kafka
Based on 2 comments
Other

Write test assertions that are both comprehensive and maintainable. Ensure all relevant fields and behaviors are validated, while using generic matchers where appropriate to reduce brittleness.

Testing Other

Reviewer Prompt

Write test assertions that are both comprehensive and maintainable. Ensure all relevant fields and behaviors are validated, while using generic matchers where appropriate to reduce brittleness.

Comprehensive Coverage: Assert all relevant fields in response objects, not just error codes. For example, when testing fetch responses, verify highWatermark, logStartOffset, and other expected fields:

// Instead of only checking error
assertEquals(Errors.NONE, responses(topicIdPartition).error)

// Also verify other relevant fields
assertEquals(Errors.NONE, responses(topicIdPartition).error)
assertEquals(highWatermark, responses(topicIdPartition).highWatermark)
assertEquals(leaderLogStartOffset, responses(topicIdPartition).logStartOffset)

Maintainable Assertions: Use generic matchers like anyLong() instead of hardcoded values when the specific value isn’t critical to the test logic:

// Instead of brittle specific values
verify(coordinator, times(0)).updateLastWrittenOffset(0)
verify(coordinator, times(1)).updateLastWrittenOffset(2)
verify(coordinator, times(1)).updateLastWrittenOffset(5)

// Use generic matchers when appropriate
verify(coordinator, times(0)).updateLastWrittenOffset(anyLong())
verify(coordinator, times(3)).updateLastWrittenOffset(anyLong())

This approach ensures tests validate the complete behavior while remaining resilient to implementation changes that don’t affect the core functionality being tested.

2
Comments Analyzed
Other
Primary Language
Testing
Category

Source Discussions